Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the precision of float values to more than 10 in python? [duplicate]

daily_raw_consumption = float(daily_rate) * float(rep_factor)

float(daily_raw_consumption)

By default rep_factor is getting converted into a precision of 10 values. For Ex:

Actual: 60.8333333333

What I need : 60.833333333333333333

Is it possible to modify the precision without converting it to Decimal

like image 1000
JustCoder Avatar asked Oct 26 '25 09:10

JustCoder


2 Answers

Consider using Decimal instead of float

from decimal import *
daily_rate = 1
rep_factor = 3
getcontext().prec = 30 # set precision to 30
daily_raw_consumption = Decimal(daily_rate) / Decimal(rep_factor)
print(daily_raw_consumption) # 0.333333333333333333333333333333
like image 134
Ian Avatar answered Oct 28 '25 21:10

Ian


 print '{0:.50f}'.format(22/7.0)

For your case

print '{0:.50f}'.format(daily_raw_consumption)
like image 36
Quazi Marufur Rahman Avatar answered Oct 28 '25 23:10

Quazi Marufur Rahman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!