Page 1 of 1

Rounding Problem with JSON Export

Posted: Thu Mar 18, 2021 9:50 am
by strongbow
I have exported my MMEX database to a JSON file. When I process the JSON file I am running into the standard rounding problems. I have tried various methods of massaging the data to enable me to reconcile the results against the MMEX database, all without success. My most recent attempt was: Multiply the MMEX transaction value by 100 to give integer values; Perform all calculations (only additions and subtractions) on integers; Divide the integer results by 100 and format for human viewing. Unfortunately this has not solved the problem Does anyone have a smart answer?

I am running MMEX Version 1.5 on Windows 10 and processing the JSON file with Python 3.9

Re: Rounding Problem with JSON Export

Posted: Thu Mar 18, 2021 4:41 pm
by MartinArmstrong
Can you give some specific examples of calculations and show the input values and the results in MMEX vs. the input values and results in your external calculation?

Re: Rounding Problem with JSON Export

Posted: Fri Mar 19, 2021 3:52 am
by dbolton
Be sure to cast as an integer in Python before doing the calculations. Otherwise the numbers still behave like floating point numbers.

Then round to two decimals right at the end when you divide by 100.

For example:

Code: Select all

amount_1 = 1.55
amount_2 = 1.66

integer_amount_1 = int(amount_1 * 100)
integer_amount_2 = int(amount_2 * 100)

integer_sum = integer_amount_1 + integer_amount_2

final_sum = round(integer_sum/100, 2)

Re: Rounding Problem with JSON Export

Posted: Sat Mar 20, 2021 4:06 pm
by strongbow
Many thanks to dbolton and MartinArmstrong for their help.

I have solved the problem, which was due to rounding on a large number of transactions and compounded by negative opening balances on a couple of accounts. My initial initial solution for the rounding issue did not cater for negative balances correctly!

Once again, many thanks for you interest and your help.

Re: Rounding Problem with JSON Export

Posted: Sun Mar 21, 2021 9:09 am
by strongbow
We can make a couple of enhancements to dbolton's suggestion:

amount_1 = 1.55
amount_2 = 1.66

# Replace following two lines
# integer_amount_1 = int(amount_1 * 100)
# integer_amount_2 = int(amount_2 * 100)

if amount_1 >= 0:
integer_amount_1 = int(amount_1 * 100 + 0.005)
else:
integer_amount_1 = int(amount_1 * 100 - 0.005)
# Repeat preceding four lines for amount_2 (or create a function)

integer_sum = integer_amount_1 + integer_amount_2

final_sum = round(integer_sum/100, 2)

Re: Rounding Problem with JSON Export

Posted: Sat Apr 10, 2021 6:15 am
by dbolton
Thanks for sharing code improvements