• 加减乘除工具类BigDecimalUtil


    场景:为毛要用BigDecimal这个类来实现加减乘除呢?我不说其他的,你去用小数乘小数,除小数就知道。话不多说,老规矩,开箱即用。

    BigDecimalUtil.java

    package com.mmall.util;
    
    import java.math.BigDecimal;
    
    /**
     * Created by 敲代码的卡卡罗特
     */
    public class BigDecimalUtil {
    
        private BigDecimalUtil(){
    
        }
    
    
        public static BigDecimal add(double v1,double v2){
            BigDecimal b1 = new BigDecimal(Double.toString(v1));
            BigDecimal b2 = new BigDecimal(Double.toString(v2));
            return b1.add(b2);
        }
    
        public static BigDecimal sub(double v1,double v2){
            BigDecimal b1 = new BigDecimal(Double.toString(v1));
            BigDecimal b2 = new BigDecimal(Double.toString(v2));
            return b1.subtract(b2);
        }
    
    
        public static BigDecimal mul(double v1,double v2){
            BigDecimal b1 = new BigDecimal(Double.toString(v1));
            BigDecimal b2 = new BigDecimal(Double.toString(v2));
            return b1.multiply(b2);
        }
    
        public static BigDecimal div(double v1,double v2){
            BigDecimal b1 = new BigDecimal(Double.toString(v1));
            BigDecimal b2 = new BigDecimal(Double.toString(v2));
            return b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//四舍五入,保留2位小数
    
            //除不尽的情况
        }
    
    
    
    
    
    }
    View Code
  • 相关阅读:
    照片墙效果一多实例演示【已封装】
    把javascript event事件封装了下,兼容大多数浏览器
    catch error
    call tcl from c
    scrollbar
    sharedlibextension
    treectrl
    get file name part
    namespace eval
    glob
  • 原文地址:https://www.cnblogs.com/coder-lzh/p/8732932.html
Copyright © 2020-2023  润新知