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

How to use BigDecimal in java

WBOY
Release: 2023-04-18 21:46:01
forward
2005 people have browsed it

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
Copy after login

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template