Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate float values?

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.

like image 710
Joan Venge Avatar asked Oct 18 '22 13:10

Joan Venge


1 Answers

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.

like image 176
Teifion Avatar answered Oct 20 '22 01:10

Teifion