Home >Java >javaTutorial >How to use BigDecimal in java

How to use BigDecimal in java

WBOY
WBOYforward
2023-04-18 21:46:012003browse

1. BigDecimal uses scale() to represent the number of decimal places.

   BigDecimal d1 = new BigDecimal("987.65");
   BigDecimal d2 = new BigDecimal("987.6500");
   BigDecimal d3 = new BigDecimal("98765400");
   System.out.println(d1.scale()); // 2,表示两位小数
   System.out.println(d2.scale()); // 4
   System.out.println(d3.scale()); // 0

2. The stripTrailingZeros() method in BigDecimal can format BigDecimal into an equal number with the 0s at the end of the value removed.

   BigDecimal d1 = new BigDecimal("123.4500");
   BigDecimal d2 = d1.stripTrailingZeros();
   System.out.println(d1+" "+d1.scale()); // 123.4500  4
   System.out.println(d2+" "+d2.scale()); // 123.45  2,因为去掉了00
   
   BigDecimal d3 = new BigDecimal("1234500");
   BigDecimal d4 = d3.stripTrailingZeros();
   System.out.println(d3+" "+d3.scale()); // 1234500  0
   System.out.println(d4+" "+d4.scale()); // 1.2345E+6  -2

The above is the detailed content of How to use BigDecimal in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete