Java float 123.129456 to 123.12 without rounding

后端 未结 5 2089
一个人的身影
一个人的身影 2020-12-20 16:11

How do you cut down float primitive in java to two decimal places, without using rounding?:

123.99999 to 123.99
-8.022222 to         


        
5条回答
  •  北海茫月
    2020-12-20 16:50

    The easy way to use DecimalFormat:

    DecimalFormat df = new DecimalFormat("0.00");
    float f = -8.0222222f;
    f = Float.parseFloat(df.format(f));
    System.out.println(f);
    

提交回复
热议问题