Floating Point with 2 Digits after Point

后端 未结 3 1080
死守一世寂寞
死守一世寂寞 2020-12-29 20:33

I will Format a Floating Point Number in android with 2 Digits after the point. I search for a Example but i din´t find one.

Can some one help me.

3条回答
  •  庸人自扰
    2020-12-29 20:51

    You can also use DecimalFormat

    float f = 102.236569f; 
    DecimalFormat decimalFormat = new DecimalFormat("#.##");
    float twoDigitsF = Float.valueOf(decimalFormat.format(f)); // output is 102.24
    //double twoDigitsF = Double.valueOf(decimalFormat.format(f)); also format double value    
    

提交回复
热议问题