How to format a float so it does not containt the remaing zeros? In other words, I want the resulting string to be as short as possible..?
Like:
3 -> "3"
3. -> "3"
3.0 -> "3"
3.1 -> "3.1"
3.14 -> "3.14"
3.140 -> "3.14"
How to format a float so it does not containt the remaing zeros? In other words, I want the resulting string to be as short as possible..?
Like:
3 -> "3"
3. -> "3"
3.0 -> "3"
3.1 -> "3.1"
3.14 -> "3.14"
3.140 -> "3.14"
If you can live with 3. and 3.0 appearing as "3.0", a very simple approach that right-strips zeros from float representations:
(thanks @ellimilial for pointing out the exceptions)
Use %g with big enough width, for example '%.99g'. It will print in fixed-point notation for any reasonably big number.
EDIT: it doesn't work