Rounding Problem with JSON Export

General discussion on all other topics under the sun.

Moderator: Renato

Post Reply
strongbow
New MMEX User
Posts: 9
Joined: Wed Feb 26, 2014 9:32 am
Are you a spam bot?: No

Rounding Problem with JSON Export

Post 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
MartinArmstrong
Super MMEX User
Posts: 29
Joined: Tue Mar 02, 2021 10:24 am
Are you a spam bot?: No

Re: Rounding Problem with JSON Export

Post 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?
dbolton
MVP MMEX User
Posts: 70
Joined: Fri Jan 03, 2020 3:24 pm
Are you a spam bot?: No

Re: Rounding Problem with JSON Export

Post 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)
strongbow
New MMEX User
Posts: 9
Joined: Wed Feb 26, 2014 9:32 am
Are you a spam bot?: No

Re: Rounding Problem with JSON Export

Post 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.
strongbow
New MMEX User
Posts: 9
Joined: Wed Feb 26, 2014 9:32 am
Are you a spam bot?: No

Re: Rounding Problem with JSON Export

Post 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)
dbolton
MVP MMEX User
Posts: 70
Joined: Fri Jan 03, 2020 3:24 pm
Are you a spam bot?: No

Re: Rounding Problem with JSON Export

Post by dbolton »

Thanks for sharing code improvements
Post Reply