简体   繁体   English

如何通过使用Java中的BigDecimals提高数字的浮点精度?

[英]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 假设我有一个字符串数字,例如1000.0,并且我想使用以下格式从其中创建BigDecimal: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?. 如何使用BigDecimals做到这一点?

Use setScale(...) , something like that: 使用setScale(...) ,类似这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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