Rohit Hazra
Rohit Hazra

Reputation: 667

Handling high precision decimals

I am pretty new to python and the only reason i have to execute a python code is because of the limitations of JavaScript's 32 bit numbers.

Situation:
I have to calculate the following to a max scale of 38 and precision of 19 Decimal Places. [MSSQL Ref: DECIMAL(38,19)]

Mathematical Operation: ((39.992 * 0.0000467788) * 0.02 / 100)

Code:

from decimal import *
import math

def truncate(f, n):
    '''Truncates/pads a float f to n decimal places without rounding'''
    s = '{}'.format(f)
    if 'e' in s or 'E' in s:
        return '{0:.{1}f}'.format(f, n)
    i, p, d = s.partition('.')
    return '.'.join([i, (d+'0'*n)[:n]])

getcontext().prec = 28

expectedTotal = "0.0000003741555539199"

vol = Decimal(39.992)
rate = Decimal(0.0000467788)
percent = Decimal(0.02)

cal1 = Decimal(truncate(((vol) * rate * percent),19))
print("(vol * rate * percent) at 19 decimal places: {}".format(cal1))

total = Decimal((vol * rate) * percent / 100)
print("total that I get: {}".format(total))
print("total that I want: {}".format(expectedTotal))

Result:

(vol * rate * percent) at 19 decimal places: 0.0000374155553919999
total that I get: 3.74155553919999983602300172E-7
total that I want: 0.0000003741555539199

Points:
1. I have no use for accuracy beyond 19 decimal places.
2. truncate function SO Link converts value to string and truncates.
3. I tried truncating to 17 decimal places then dividing by 100, but didnt work.

Upvotes: 0

Views: 327

Answers (1)

rakwaht
rakwaht

Reputation: 3967

You can supress the scientific notation in Python:

x = 3.74155553919999983602300172E-7
print('{:f}'.format(x))
>> 0.000000
print('{:.20f}'.format(x))
>> 0.00000037415555392000

Note that the default precision is 6 however in the example case is not enough so you can express your precision like I did in the second test.

NB: that if you set a specific number Python is going to fill in with zeros at the end if needed.

Upvotes: 1

Related Questions