How many decimal Places in A Double (Java)

前端 未结 7 1006
南方客
南方客 2020-11-30 14:19

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         


        
7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 14:46

    No.

    1.100 and 1.1 are exactly the same value (they are represented exactly the same bit-for-bit in a double).

    Therefore you can't ever get that kind of information from a double.

    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.

提交回复
热议问题