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
Rounding Problem with JSON Export
Moderator: Renato
-
- New User
- Posts: 19
- Joined: Wed Feb 26, 2014 9:32 am
- Are you a spam bot?: No
-
- MVP User
- Posts: 300
- Joined: Tue Mar 02, 2021 10:24 am
- Are you a spam bot?: No
Re: Rounding Problem with JSON Export
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?
-
- Super User
- Posts: 126
- Joined: Fri Jan 03, 2020 3:24 pm
- Are you a spam bot?: No
Re: Rounding Problem with JSON Export
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:
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)
-
- New User
- Posts: 19
- Joined: Wed Feb 26, 2014 9:32 am
- Are you a spam bot?: No
Re: Rounding Problem with JSON Export
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.
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.
-
- New User
- Posts: 19
- Joined: Wed Feb 26, 2014 9:32 am
- Are you a spam bot?: No
Re: Rounding Problem with JSON Export
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)
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)
-
- Super User
- Posts: 126
- Joined: Fri Jan 03, 2020 3:24 pm
- Are you a spam bot?: No
Re: Rounding Problem with JSON Export
Thanks for sharing code improvements