简体   繁体   中英

How to increase the floating point precision of a number by using BigDecimals in java?

Let's assume I have a string number such as 1000.0 and I want to create BigDecimal out of it in the following format: 1000.00

edit: And also if the string number already has a floating precision of two decimals, don't touch it.

How can I do this by using BigDecimals?.

Use setScale(...) , something like that:

BigDecimal result = new BigDecimal("1000.0");
result.setScale(2, RoundingMode.HALF_UP);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM