bigone发行的虚拟货币(big虚拟币)

频道:百科知识 日期: 浏览:0

Bitget下载

注册下载Bitget下载,邀请好友,即有机会赢取 3,000 USDT

APP下载   官网注册

BigDecimal,ROUND_UP的进位制

java.math包中提供的API类BigDecimal,对超过16位有效位的数进行精确的运算。BigDecimal.ROUND_UP,进位制:不管保留数字后面是大是小(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    }}

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

商品单位的价格换算工具

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

import java.math.BigDecimal;import java.util.LinkedHashMap;public class UnitPriceTools {    /**     * 设置一个单位的价格,自动生成所有的价格     *     * @param unitGroupMap     * @param unitType     * @param unitPrice     * @param model     * @return     */    public static LinkedHashMap<Integer, BigDecimal> toUnitPriceMapWithType(LinkedHashMap<Integer, Unit> unitGroupMap, int unitType, BigDecimal unitPrice, int model) {        LinkedHashMap<Integer, BigDecimal> 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<Integer, BigDecimal> toUnitPriceMapWithType(LinkedHashMap<Integer, Unit> 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<Unit> unitList = UnitConvertService.getUnitList(goodsId);        // =======================================================================================//        // >>>>>>>>>>>> 按类型分组返回保证顺序的Map(根据单位类型)        LinkedHashMap<Integer, Unit> unitGroupMap = UnitConvertTools.toUnitMapWithType(goodsId, unitList);        System.out.println("按类型分组返回保证顺序的Map(根据单位类型) = " + unitGroupMap);        LinkedHashMap<Integer, BigDecimal> unitType1PriceMap = UnitPriceTools.toUnitPriceMapWithType(unitGroupMap, 1, new BigDecimal("1.11"));        System.out.println("单位类型1,价格 = " + unitType1PriceMap);        LinkedHashMap<Integer, BigDecimal> unitType2PriceMap = UnitPriceTools.toUnitPriceMapWithType(unitGroupMap, 2, new BigDecimal("44.14"), 0);        System.out.println("单位类型2,价格 = " + unitType2PriceMap);        LinkedHashMap<Integer, BigDecimal> unitType3PriceMap = UnitPriceTools.toUnitPriceMapWithType(unitGroupMap, 3, new BigDecimal("444.10"), 0);        System.out.println("单位类型3,价格 = " + unitType3PriceMap);        LinkedHashMap<Integer, BigDecimal> 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}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 931614094@qq.com 举报,一经查实,本站将立刻删除。

本文地址: http://www.lyw520.com/baikezhishi/9238.html
文章来源: 小美
bigone发行的虚拟货币(big虚拟币)文档下载: PDF DOC TXT