3

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

JustCoder
  • 327
  • 1
  • 4
  • 13
  • 1
    Answered here: http://stackoverflow.com/questions/11522933/python-floating-point-arbitrary-precision-available – abe Feb 25 '16 at 05:47

2 Answers2

4

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
Rick
  • 7,007
  • 2
  • 49
  • 79
Ian
  • 30,182
  • 19
  • 69
  • 107
  • Is it possible to do this without using the decimal conversion. I am looking for that type of suggestions. – JustCoder Feb 25 '16 at 09:05
  • @JustCoder ah, I see.. nope. :( the digit precision in your example is 20 digit: `60.833333333333333333`, there is no way `float` can be as precise as 20-digit. Its max precision is about 15 to 16 digit: `60.8333333333333`. Since you already have 2 digits in front of dot, what you can do, at most, is to have 13-digit after dot to retain its precision: `print ('{0:.13f}'.format(daily_raw_consumption))` you cannot be more precise than this by using `float`. Only `decimal` can go up to 28 digit precision. – Ian Feb 25 '16 at 09:08
3
 print '{0:.50f}'.format(22/7.0)

For your case

print '{0:.50f}'.format(daily_raw_consumption)
Quazi Marufur Rahman
  • 2,603
  • 5
  • 33
  • 51