How to truncate float values?

Joan Venge picture Joan Venge · Apr 24, 2009 · Viewed 240.8k times · Source

I want to remove digits from a float to have a fixed number of digits after the dot, like:

1.923328437452 → 1.923

I need to output as a string to another function, not print.

Also I want to ignore the lost digits, not round them.

Answer

Teifion picture Teifion · Apr 24, 2009
round(1.923328437452, 3)

See Python's documentation on the standard types. You'll need to scroll down a bit to get to the round function. Essentially the second number says how many decimal places to round it to.