0

I'm trying to truncate (round down) my f-string formatted float. My original float variable with value 700.75 becomes 700.8 after using f-string with a precision of 1. For ex., I use f-string as below.

quantity = float(f'{quantity:.1f}')

My desired value is 700.7 with precision one. How can I achieve that while still using f-strings?

Victor Cui
  • 1,393
  • 2
  • 15
  • 35
  • 3
    Watch out - that kind of rounding is trickier than you might think, because floats are binary, not decimal. For example, the float normally displayed as `0.3` actually has value `0.299999999999999988897769753748434595763683319091796875`, and if you try to display that to 1 digit after the decimal point, rounded down, you'll see `0.2`, not `0.3`. – user2357112 Jun 21 '22 at 03:58
  • Take a look at the second answer of https://stackoverflow.com/questions/783897/how-to-truncate-float-values – Chris Jun 21 '22 at 04:00
  • Why do you need to use f-strings for this? Does `math.floor(quantity * 100) / 100` not work? – Quelklef Jun 21 '22 at 04:05
  • Does this answer your question? [How to truncate float values?](https://stackoverflow.com/questions/783897/how-to-truncate-float-values) – SuperStormer Jun 21 '22 at 04:13

0 Answers0