BigDecimaljava字符串转化为数字,ROUND_UP的进位制

java.math包中提供的API类BigDecimal,对超过16位有效位的数进行精确的运算。BigDecimal.ROUND_UPjava字符串转化为数字,进位制:不管保留数字后面是大是小(0除外)都会进1。

案例代码:

public class BigDecimalDemo {
    public static void main(String[] args) {
        BigDecimal numOne = new BigDecimal("3.14959267");
        BigDecimal numTwo = new BigDecimal("3.3205667");
        BigDecimal numThree = new BigDecimal("3.3005667");
        BigDecimal numOneRound = numOne.setScale(2, BigDecimal.ROUND_UP);
        System.out.println("numOneRound:" + numOneRound);  // 3.15
        BigDecimal numTwoRound = numTwo.setScale(2, BigDecimal.ROUND_UP);
        System.out.println("numTwoRound:" + numTwoRound); // 3.33
        BigDecimal numThreeRound = numThree.setScale(2, BigDecimal.ROUND_UP);
        System.out.println("numThreeRound:" + numThreeRound); // 3.31
    }
}

java字符转数字_c 判断字符是否为数字_java字符串转化为数字

参考: 金钱及货币使用的数据类型BigDecimal

商品单位的价格换算工具

之前 商品单位转换及单位换算功能 的基础上,商品单位设定其中一种类型的价格,其他价格已同时设定。

import java.math.BigDecimal;
import java.util.LinkedHashMap;
public class UnitPriceTools {
    /**
     * 设置一个单位的价格,自动生成所有的价格
     *
     * @param unitGroupMap
     * @param unitType
     * @param unitPrice
     * @param model
     * @return
     */
    public static LinkedHashMap toUnitPriceMapWithType(LinkedHashMap unitGroupMap, int unitType, BigDecimal unitPrice, int model) {
        LinkedHashMap unitPriceMap = new LinkedHashMap();
        if (unitType == 1) {
            // 单位类型1
            unitPriceMap.put(1, unitPrice.setScale(2));
            // 单位类型2
            BigDecimal unitPrice2 = unitPrice.multiply(new BigDecimal(unitGroupMap.get(2).getUnitValue())).setScale(2);
            unitPriceMap.put(2, unitPrice2);
            // 单位类型3
            BigDecimal unitPrice3 = unitPrice2.multiply(new BigDecimal(unitGroupMap.get(3).getUnitValue())).setScale(2);
            unitPriceMap.put(3, unitPrice3);
            // 单位类型4
            BigDecimal unitPrice4 = unitPrice3.multiply(new BigDecimal(unitGroupMap.get(4).getUnitValue())).setScale(2);
            unitPriceMap.put(4, unitPrice4);
        } else if (unitType == 2) {
            // 单位类型1
            BigDecimal unit1Price = unitPrice.divide(new BigDecimal(unitGroupMap.get(2).getUnitValue()));
            BigDecimal unit1RoundPrice = unit1Price.setScale(2, BigDecimal.ROUND_UP);
            if (model == 0) {
                unitPriceMap.put(11, unit1Price);
                unitPriceMap.put(1, unit1RoundPrice);
            } else {
                unitPriceMap.put(1, unit1RoundPrice);
            }
            // 单位类型2
            unitPriceMap.put(2, unitPrice.setScale(2));
            // 单位类型3
            BigDecimal unitPrice3 = unitPrice.multiply(new BigDecimal(unitGroupMap.get(3).getUnitValue())).setScale(2);
            unitPriceMap.put(3, unitPrice3);
            // 单位类型4
            BigDecimal unitPrice4 = unitPrice3.multiply(new BigDecimal(unitGroupMap.get(4).getUnitValue())).setScale(2);
            unitPriceMap.put(4, unitPrice4);
        } else if (unitType == 3) {
            // 单位类型2
            BigDecimal unit2Price = unitPrice.divide(new BigDecimal(unitGroupMap.get(3).getUnitValue()));
            BigDecimal unit2RoundPrice = unit2Price.setScale(2, BigDecimal.ROUND_UP);
            if (model == 0) {
                unitPriceMap.put(21, unit2Price);
                unitPriceMap.put(2, unit2RoundPrice);
            } else {
                unitPriceMap.put(2, unit2RoundPrice);
            }
            // 单位类型1
            BigDecimal unit1Price = unit2Price.divide(new BigDecimal(unitGroupMap.get(2).getUnitValue()));
            BigDecimal unit1RoundPrice = unit1Price.setScale(2, BigDecimal.ROUND_UP);
            if (model == 0) {
                unitPriceMap.put(11, unit1Price);
                unitPriceMap.put(1, unit1RoundPrice);
            } else {
                unitPriceMap.put(1, unit1RoundPrice);
            }
            // 单位类型3
            unitPriceMap.put(3, unitPrice.setScale(2));
            // 单位类型4
            BigDecimal unitPrice4 = unitPrice.multiply(new BigDecimal(unitGroupMap.get(4).getUnitValue())).setScale(2);
            unitPriceMap.put(4, unitPrice4);
        } else if (unitType == 4) {
            // 单位类型3
            BigDecimal unit3Price = unitPrice.divide(new BigDecimal(unitGroupMap.get(4).getUnitValue()));
            BigDecimal unit3RoundPrice = unit3Price.setScale(2, BigDecimal.ROUND_UP);
            if (model == 0) {
                unitPriceMap.put(31, unit3Price);
                unitPriceMap.put(3, unit3RoundPrice);
            } else {
                unitPriceMap.put(3, unit3RoundPrice);
            }
            // 单位类型2
            BigDecimal unit2Price = unit3Price.divide(new BigDecimal(unitGroupMap.get(3).getUnitValue()));
            BigDecimal unit2RoundPrice = unit2Price.setScale(2, BigDecimal.ROUND_UP);
            if (model == 0) {
                unitPriceMap.put(21, unit2Price);
                unitPriceMap.put(2, unit2RoundPrice);
            } else {
                unitPriceMap.put(2, unit2RoundPrice);
            }
            // 单位类型1
            BigDecimal unit1Price = unit2Price.divide(new BigDecimal(unitGroupMap.get(2).getUnitValue()));
            BigDecimal unit1RoundPrice = unit1Price.setScale(2, BigDecimal.ROUND_UP);
            if (model == 0) {
                unitPriceMap.put(11, unit1Price);
                unitPriceMap.put(1, unit1RoundPrice);
            } else {
                unitPriceMap.put(1, unit1RoundPrice);
            }
            // 单位类型4
            unitPriceMap.put(4, unitPrice.setScale(2));
        }
        return unitPriceMap;
    }
    /**
     * @param unitGroupMap
     * @param unitType
     * @param unitPrice
     * @return
     */
    public static LinkedHashMap toUnitPriceMapWithType(LinkedHashMap unitGroupMap, int unitType, BigDecimal unitPrice) {
        return toUnitPriceMapWithType(unitGroupMap, unitType, unitPrice, 1);
    }
}

工具类测试:

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
public class UnitPriceToolsTest {
    public static void main(String[] args) {
        // 准备商品
        Long goodsId = 1000L;
        // 准备商品单位列表
        List unitList = UnitConvertService.getUnitList(goodsId);
        // =======================================================================================//
        // >>>>>>>>>>>> 按类型分组返回保证顺序的Map(根据单位类型)
        LinkedHashMap unitGroupMap = UnitConvertTools.toUnitMapWithType(goodsId, unitList);
        System.out.println("按类型分组返回保证顺序的Map(根据单位类型) = " + unitGroupMap);
        LinkedHashMap unitType1PriceMap = UnitPriceTools.toUnitPriceMapWithType(unitGroupMap, 1, new BigDecimal("1.11"));
        System.out.println("单位类型1,价格 = " + unitType1PriceMap);
        LinkedHashMap unitType2PriceMap = UnitPriceTools.toUnitPriceMapWithType(unitGroupMap, 2, new BigDecimal("44.14"), 0);
        System.out.println("单位类型2,价格 = " + unitType2PriceMap);
        LinkedHashMap unitType3PriceMap = UnitPriceTools.toUnitPriceMapWithType(unitGroupMap, 3, new BigDecimal("444.10"), 0);
        System.out.println("单位类型3,价格 = " + unitType3PriceMap);
        LinkedHashMap unitType4PriceMap = UnitPriceTools.toUnitPriceMapWithType(unitGroupMap, 4, new BigDecimal("8888"), 0);
        System.out.println("单位类型4,价格 = " + unitType4PriceMap);
    }
}

测试输出内容:

单位类型1,价格 = {1=1.11, 2=44.40, 3=444.00, 4=8880.00}
单位类型2,价格 = {11=1.1035, 1=1.11, 2=44.14, 3=441.40, 4=8828.00}
单位类型3,价格 = {21=44.41, 2=44.41, 11=1.11025, 1=1.12, 3=444.10, 4=8882.00}
单位类型4,价格 = {31=444.4, 3=444.40, 21=44.44, 2=44.44, 11=1.111, 1=1.12, 4=8888.00}

限时特惠:本站每日持续更新海量设计资源,一年会员只需29.9元,全站资源免费下载
站长微信:ziyuanshu688