I would like to keep the total number of digits (before and after the decimal point) of a float constant in python.
For example, if I want to impose a fixed width of 7: 1234.567890123 would become 1234.567 but 12345.678901234 would become 12345.67
Fixing the number of decimals does not work in this case since it depends on how many digits I have before the decimal point. I also tried the [width] option but it impose a minimum width and I need a maximum.
Thanks for your input!
Just by using your example,
a = 1234.567890123
b = 12345.678901234
str(a)[:8] # gives '1234.567'
str(b)[:8] # gives '12345.67'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With