如何使用Java计算货币/钱~(How to calculate monetary values in Java)

There are many monetary values calculation in the financial or e-commerce application, and there is one question that arises for this – Should we use double or float data type to represent the monetary values?

在金融或电子商务应用程序中有许多货币价值的计算,因此出现了一个问题——我们应该使用double或float数据类型来表示货币价值吗?

Answer: Always uses java.math.BigDecimal to represent the monetary values.

答案:总是使用java.math.BigDecimal 表示货币值。

1. Double or Float?

Here is an example of using double and float to represent the monetary values in Java.

下面是一个使用double和float来表示Java中的货币值的例子。

 

 

 

 1 package moneycount;
 2 
 3 import java.text.DecimalFormat;
 4 
 5 public class JavaMoney {
 6     private static DecimalFormat df = new DecimalFormat("0.00");
 7 
 8     public static void main(String[] args) {
 9         System.out.println(2.00 - 1.1);//0.8999999999999999
10         System.out.println(2.00 - 1.2);//0.8
11         System.out.println(2.00 - 1.3);//0.7
12         System.out.println(2.00 - 1.4);//0.6000000000000001
13         System.out.println(2.00 - 1.5);//0.5
14         System.out.println(2.00 - 1.6);//0.3999999999999999
15         System.out.println(2.00 - 1.7);//0.30000000000000004
16         System.out.println(2.00 - 1.8);//0.19999999999999996
17         System.out.println(2.00 - 1.9);//0.10000000000000009
18         System.out.println(2.00 - 2);//0.0
19 
20         System.out.println("--------------------");
21 
22         double i = 2.0;
23         double j = 1.7;
24 
25         System.out.println("double : " + (i - j));//double : 0.30000000000000004
26         System.out.println("double : " + df.format(i - j));//double : 0.30
27 
28         float i2 = 2.0f;
29         float j2 = 1.7f;
30 
31         System.out.println("float : " + (i2 - j2));//float : 0.29999995
32         System.out.println("float : " + df.format(i2 - j2));//float : 0.30
33     }
34 }

运行结果

 

 1 --------------------
 2 0.8999999999999999
 3 0.8
 4 0.7
 5 0.6000000000000001
 6 0.5
 7 0.3999999999999999
 8 0.30000000000000004
 9 0.19999999999999996
10 0.10000000000000009
11 0.0
12 --------------------
13 double : 0.30000000000000004
14 double : 0.30
15 float : 0.29999995
16 float : 0.30

 

 

 

结果——可以看出,使用了DecimalFormat 适配后,便会得出想要的结果。

通过查看BigDecimal类,发现其构造函数,可传入的值类型有6种,

BigDecimal(BigInteger val)

1 public BigDecimal(BigInteger val) {
2     scale = 0;     //private final int scale;
3     intVal = val;  //private final BigInteger intVal;
4     intCompact = compactValFor(val); //private final transient long intCompact;
5 }

BigDecimal(char[] in)

public BigDecimal(char[] in) {
    this(in, 0, in.length);
}

BigDecimal(double val)

public BigDecimal(double val) {
    this(val,MathContext.UNLIMITED);
}

BigDecimal(int val)

public BigDecimal(int val) {
    this.intCompact = val;
    this.scale = 0;
    this.intVal = null;
}

BigDecimal(long val)

public BigDecimal(long val) {
    this.intCompact = val;
    this.intVal = (val == INFLATED) ? INFLATED_BIGINT : null;
    this.scale = 0;
}

BigDecimal(String val)

public BigDecimal(String val) {
    this(val.toCharArray(), 0, val.length());
}

2. String or Other?

测试创建实例,使用int和String创建实例

 1 package moneycount;
 2 
 3 import java.math.BigDecimal;
 4 import java.text.DecimalFormat;
 5 
 6 public class JavaMoney {
 7     private static DecimalFormat df = new DecimalFormat("0.00");
 8 
 9     public static void main(String[] args) {
10         BigDecimal num1 = new BigDecimal(0.001);
11         BigDecimal num2 = new BigDecimal(0.5);
12         BigDecimal num3 = new BigDecimal(100);
13         BigDecimal num4 = new BigDecimal(-5);
14 
15         BigDecimal nums1 = new BigDecimal("0.001");
16         BigDecimal nums2 = new BigDecimal("0.5");
17         BigDecimal nums3 = new BigDecimal("100");
18         BigDecimal nums4 = new BigDecimal("-5");
19 
20         //加法
21         BigDecimal result1 = num1.add(num2);
22         BigDecimal results1 = nums1.add(num2);
23         System.out.println("result1="+result1);
24         System.out.println("results1="+results1);
25         //减法
26         BigDecimal result2 = num3.add(num2);
27         BigDecimal results2 = nums3.add(num2);
28         System.out.println("result2="+result2);
29         System.out.println("results2="+results2);
30         //乘法
31         BigDecimal result3 = num1.add(num3);
32         BigDecimal results3 = nums1.add(num3);
33         System.out.println("result3="+result3);
34         System.out.println("results3="+results3);
35         //除法
36         BigDecimal result4 = num3.divide(num2);
37         BigDecimal results4 = nums3.divide(num2);
38         System.out.println("result4="+result4);
39         System.out.println("results4="+results4);
40         //绝对值
41         BigDecimal result5 = num4.abs();
42         BigDecimal results5 = nums4.abs();
43         System.out.println("result5="+result5);
44         System.out.println("results5="+results5);
45     }
46 }

运行结果

 

得出结论,建议使用String创建 BigDecimal实例。

 

posted @ 2020-04-10 21:49  厸清扬  阅读(702)  评论(0编辑  收藏  举报