Felka98
Felka98

Reputation: 109

Output a number up to N decimal places withoud rounding

If i have the number 1.2348 and I used the round function to round it to 3 decimal places, I would get 1.235 but I want to get 1.234. If i did:

num=1.2348
print(str(num)[:5])

I would get a string and if I tried to convert that string to an int back again(because I need it to be an int) it raises a ValueError.

Upvotes: 0

Views: 33

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49921

This will do what you want:

>>> int(num*1000)/1000.0
1.234

Unless you encounter a round-off error.

Upvotes: 2

Related Questions