java - How to round floats slightly and cut off trailing zeroes when printing them without using a custom formatter? -


the thing need print numbers "0.019999979000" "0.02".

the problem using string.format() numbers either printed "0.019999979" (%s) or "0.020000000" (%f).

is there way combine effects without string manipulations?

upd: numbers not contain 2 digits after point - that's example show values close rounded

you seem looking number format based on number of significant digits. say, n = 2, want display number rounded first 3 significant digits, i.e.

1234     -> 1200 0.001234 -> 0.0012 1.999    -> 2.0 0.019999979000 -> 0.02 

in case, unfortunately, official java libraries won't (a glaring omission imo). under android @ least, can use

new decimalformat("@@@") 

for 3 significant digits; otherwise, need available implementations. 1 know of icu4j (international components unicode), have not evaluated it.


Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -