創(chuàng)建BigDecimal對(duì)象
創(chuàng)建BigDecimal對(duì)象常用的方式有2種:
BigDecimal a = BigDecimal.new Bigdecimal(1);
BigDecimal a1 = BigDecimal.valueOf(1);
不推薦使用new 關(guān)鍵字創(chuàng)建Bigdecimal對(duì)象蛇损。原因是當(dāng)new 的值是一個(gè)小數(shù)的時(shí)候,其真實(shí)的值并不是我們想要的值
BigDecimal b = new BigDecimal(1.1);
BigDecimal b1 = BigDecimal.valueOf(1.1);
System.out.println("new的值b: "+b);
System.out.println("valueOf()的值b1: "+b1);
輸出:
1.100000000000000088817841970012523233890533447265625
valueOf()的值b1: 1.1
這在比較大小的時(shí)候,很可能出現(xiàn)意想不到的結(jié)果霸旗。
加法
加法的方法有兩種芝囤,方法簽名如下:
public BigDecimal add(BigDecimal augend)
public BigDecimal add(BigDecimal augend, MathContext mc)
demo:
BigDecimal a = BigDecimal.valueOf(1);
BigDecimal b = BigDecimal.valueOf(1.5);
BigDecimal c = BigDecimal.valueOf(1.4);
BigDecimal addResult = a.add(b);
BigDecimal addResult1 = a.add(b,new MathContext(2));
BigDecimal addResult2 = a.add(b,new MathContext(1));
BigDecimal addResult3 = a.add(c,new MathContext(1));
System.out.println("addResult: "+addResult);
System.out.println("addResult1: "+addResult1);
System.out.println("addResult2: "+addResult2);
System.out.println("addResult3: "+addResult3);
result:
addResult: 2.5
addResult1: 2.5
addResult2: 3
addResult3: 2
mc 設(shè)置保留幾位小數(shù),舍入按四舍五入
減法
方法簽名如下:
public BigDecimal subtract(BigDecimal subtrahend)
public BigDecimal subtract(BigDecimal subtrahend, MathContext mc)
demo:
BigDecimal a = BigDecimal.valueOf(1);
BigDecimal b = BigDecimal.valueOf(1.5);
BigDecimal c = BigDecimal.valueOf(1.4);
BigDecimal subtract = a.subtract(b);
BigDecimal subtract1 = a.subtract(b,new MathContext(2));
BigDecimal subtract2 = a.subtract(b,new MathContext(1));
BigDecimal subtract3 = a.subtract(c,new MathContext(1));
System.out.println("subtract: "+subtract);
System.out.println("subtract1: "+subtract1);
System.out.println("subtract2: "+subtract2);
System.out.println("subtract3: "+subtract3);
result:
subtract: -0.5
subtract1: -0.5
subtract2: -0.5
subtract3: -0.4
乘法:
方法簽名:
public BigDecimal multiply(BigDecimal multiplicand)
public BigDecimal multiply(BigDecimal multiplicand, MathContext mc)
demo:
BigDecimal a = BigDecimal.valueOf(1);
BigDecimal b = BigDecimal.valueOf(1.5);
BigDecimal c = BigDecimal.valueOf(1.4);
BigDecimal multiply = a.multiply(b);
BigDecimal multiply1 = a.multiply(b,new MathContext(2));
BigDecimal multiply2 = a.multiply(b,new MathContext(1));
BigDecimal multiply3 = a.multiply(c,new MathContext(1));
System.out.println("multiply: "+multiply);
System.out.println("multiply1: "+multiply1);
System.out.println("multiply2: "+multiply2);
System.out.println("multiply3: "+multiply3);
result:
multiply: 1.5
multiply1: 1.5
multiply2: 2
multiply3: 1
除法:
public BigDecimal divide(BigDecimal divisor)
public BigDecimal divide(BigDecimal divisor, MathContext mc)
public BigDecimal divide(BigDecimal divisor, int roundingMode)
public BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode)
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
其中 public BigDecimal divide(BigDecimal divisor) 不推薦使用犬缨。idea警告如下:
Inspection info: Reports calls to divide() or setScale() without a rounding mode argument. Such calls can lead to an ArithmeticException when the exact value cannot be represented in the result (e.g. because it has a non-terminating decimal expansion). Specifying a rounding mode prevents the ArithmeticException.
翻譯:
檢查信息:報(bào)告調(diào)用divide()或setScale(),而不帶舍入模式參數(shù)棉浸。當(dāng)結(jié)果中不能表示精確值時(shí)怀薛,這種調(diào)用可能導(dǎo)致算術(shù)異常(例如,因?yàn)樗哂袩o限的十進(jìn)制展開)迷郑。指定舍入模式可以防止算術(shù)異常枝恋。
簡(jiǎn)單點(diǎn)說就是除不盡的時(shí)候會(huì)報(bào)錯(cuò)
所以用除法的時(shí)候创倔,我們需要指定其舍入模式。推薦使用:
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
指定舍入模式焚碌,和保留幾位小數(shù)
demo:
BigDecimal divide = a.divide(b, 2, BigDecimal.ROUND_HALF_UP);
System.out.println("divide: "+divide);