0

I have the following code:

limited="{:0.{}f}".format(0.32244647, 5)
print (limited)

it shows '0.32245'. i expected it to only limit the floating digits by 5 not round them.
I need to have 0.3224. i used round function as well but its function was like this.

Mehrdad Dadvand
  • 340
  • 4
  • 19
  • It's kind of surprising that the formatting isn't flexible enough to let you do this directly. – Mark Ransom Dec 18 '19 at 19:06
  • when doing `format(myFloat, ".5f")` it will round because of the purpose of precision. It wants to give you the most accurate information. If you are looking to just truncate the float, you would need to do: `math.floor(myFloat * PLACES) / PLACES` where PLACES is 10\*\*the number of places (10\*\*5) in your case. – Fallenreaper Dec 18 '19 at 19:57
  • https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points – Fallenreaper Dec 18 '19 at 19:58
  • 1
    @Fallenreaper I would use `math.trunc` rather than `math.floor`, it produces the expected result with negative numbers. – Mark Ransom Dec 18 '19 at 23:04
  • Good call @MarkRansom – Fallenreaper Dec 19 '19 at 01:03

0 Answers0