A Double(Java)中的小数位数 [英] How many decimal Places in A Double (Java)
问题描述
在java中是否有任何内置函数告诉我double中有多少小数位。例如:
Is there any in built function in java to tell me how many decimal places in a double. For example:
101.13 = 2
101.130 = 3
1.100 = 3
1.1 = 1
-3.2322 = 4 etc.
我很乐意首先转换为另一种类型需要,我已经考虑过转换为bigdecimal而没有运气。
I am happy to convert to another type first if needed, I have looked at converting to bigdecimal first with no luck.
推荐答案
否。
1.100和1.1是完全相同的值(它们在 double
中表示完全相同)。
1.100 and 1.1 are exactly the same value (they are represented exactly the same bit-for-bit in a double
).
因此,您无法从 double
获得此类信息。
Therefore you can't ever get that kind of information from a double
.
你可以做的唯一事情是获得将十进制数解析为相同的 double
值所需的最小小数位数。这就像调用 Double.toString()
一样简单,并检查它有多少位数。
The only thing you can do is to get the minimum number of decimal digits necessary for a decimal number to be parsed into the same double
value. And that is as easy as calling Double.toString()
and checking how many decimal digits there are.
这篇关于A Double(Java)中的小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!