I have this code in VBA that needs to be in Python. I'm new to Python, sorry for that.
Sub cdec_test()
Dim val1, val2, result1, result2, x1, x2 As Variant
val = CDec(5930 / 3000) 'val1 : 1.97666666666667 : Variant/Decimal
result = 0.63 + (0.48 - 0.63) * (val1 - 1)'result1 : 0.4834999999999995 : Variant/Decimal
x = Application.WorksheetFunction.Round(result1, 3) '0.483
End Sub
In Python, however, result
for some reason rounded to 0.4835
, and x
will be rounded further to 0.484
. Here is my Python code
def cdec_test():
getcontext().prec = 28
val = Decimal('5930') / Decimal('3000') # val : 1.9766666666666667 , so far so good
result = Decimal('0.63') + (Decimal('0.48') - Decimal('0.63')) * (val - Decimal('1')) #'0.4835000000000000000000000000'
return result
result = cdec_test()
Tried changing getcontext().prec = 15
to match numbers of digits, no luck.
Later in code this is getting multiplied by 10^6..10^9 numbers so it's a big deal... How do I fix it? Does Python have a regular C-style double format? What modules can help?
CDec(5390/3000)
byCDec(5390)/CDec(3000)
the result flips from.483
to.484
– Didactic5.390
and3.000
have no further allocated memory for fractions and requesting an output fortype as decimal
does not impart added fractions. – Asare