Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get a float with desired precision using python

Tags:

python

for example, i want to get a float with the initial 3 digits from e (2.71 from 2.718281828...), is there any function i can use ? i have tried round(), math.floor() and Decimal(1).exp() with getcontext().prec=1, but none of them gives me what i want (round() gives me 2.72, math.floor() 2 and Decimal(1).exp also 2.72) i dont want to round up the float.

thanks in advance.

like image 839
Ema Avatar asked Dec 04 '22 02:12

Ema


1 Answers

round(x - 0.005, 2)
# => 2.71
math.floor(x * 100) / 100
# => 2.71
like image 100
Amadan Avatar answered Dec 24 '22 06:12

Amadan