in C#, I can do (#.####), which prints up to 4 significant digits after the decimal point.
1.2 -> 1.2
1.234 -> 1.234
1.23456789 -> 1.2345
Afaik, in python, there is only the c-style %.4f which will always print to 4 decimal points padded with 0s at the end if needed.
I don’t want those 0s.
Any suggestions for what is the cleanest way to achieve what I need?
One possible solution is to print it first and trim ending 0s myself, but hoping to find more clever ways.
It looks like you can use the “g” format specifier for the
.format()string function (only available in python 2.6+):This includes digits before and after the decimal point, which is why you get 1.235 (rounded 1.2345 to 4 digits). If you only want significant digits to the right, you’d have to do something funny like
Note that it still rounds, unlike your example.