Java keep trailing 0 in float operations

前端 未结 3 1375
南方客
南方客 2021-01-28 03:56

the code:

Float f = Float.parseFloat(\"1.80\");
System.out.println(f);

prints \"1.8\" on screen. I need to keep the 0 in the float

3条回答
  •  盖世英雄少女心
    2021-01-28 04:01

    1. Floating point variables don't have decimal places. They have binary places.
    2. 1.8 and 1.80 are the same number, and they are represented the same way in a float or double.
    3. If you want them presented with a certain number of decimal places, you have to convert to a decimal radix, via either BigDecimal or DecimalFormat, where you can control the number of decimal places.

    In short the question doesn't really make sense as posed.

提交回复
热议问题