Floating Point with 2 Digits after Point

后端 未结 3 1056
死守一世寂寞
死守一世寂寞 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

    In answer above it always will return 2 deciaml

    examples with String.format("%.02f", f)

    2.5555 -> 2.55

    2.5 -> 2.50

    but do you need to get 2.50 for 2.5?

    for avoid to get excessive 0, you can use this solution

    val format = DecimalFormat("0.##")
    return format.format(floatNum)
    

提交回复
热议问题