0

I am developing an accounting system where another system sends data to my accounting software. the other system uses double-precision while my accounting system uses decimal with rounding up to 2 decimal places. the problem occurs after the rounding is done, the totals representing the debit side and credit side are not equal anymore due to differences around 0.01 or 0.02...or so due to the rounding.

This makes it impossible to save the voucher to update accounts. I am wondering what could be a professional solution to that problem.

Any help is appreciated as this issue is occurring in production.

KaiserKatze
  • 1,521
  • 2
  • 20
  • 30
  • Welcome to Stackoverflow! It is better to think of this community like you do for Wikipedia. Users come here with a specific question in mind, looking for a specific answer. Therefore, we require a description of the problem, your own attempt at solving the problem, the errors you are getting and what the expected output is. If those boxes are ticked, some kind volunteer may help you out. Please refer to this for more information https://stackoverflow.com/help/how-to-ask I hope you have a good time here! – Aman Dec 29 '20 at 13:05
  • 3
    @Aman: Stack Overflow does not **require** those things. They depend on the question. Debugging questions, for example, generally call for a [mre]. But a question about interpreting a specific rule of the C standard does not. This one needs more information, but it is not apparent that an “attempt at solving the problem” is required. – Eric Postpischil Dec 29 '20 at 13:41
  • @EricPostpischil, thank you, that makes sense! I'll keep this in mind from now on. – Aman Dec 29 '20 at 13:42
  • If the other system had amounts of currency that were whole numbers of cents or other unit, and you rounded all numbers you received to whole numbers of the same unit and did additions with no loss of accuracy (e.g., not using decimal floating-point where the significand was insufficient for complete representation of a sum), and the numbers in the original system added up equally on the debit side and the credit side, then your sums would add up equally on the debit side and the credit side. If they do not, one of those criteria is violated, and the task is to figure out which. – Eric Postpischil Dec 29 '20 at 13:44
  • 1
    Adding one more requirement: The numbers in the other system are not so large that the `double` nearest a whole number of units is not nearer to another whole number of units. For cents, this means the numbers are less than 90 trillion dollars or so. So, (a) Are the numbers you are receiving whole numbers of cents or another unit, as nearly as the `double` format can represent them? Or are some of them fractional cents, like $3.4579 (= 345.79¢)? (b) Do you immediately round the `double` values to whole numbers of cents or another unit? (c) Do the original numbers add up equally? – Eric Postpischil Dec 29 '20 at 13:48
  • Another requirement is bounds on the number of numbers being added, so that the rounding errors do not accumulate so much. I may make a more formal answer, but it would help if you could address the requirements so far. – Eric Postpischil Dec 29 '20 at 14:21
  • How about using a [fixed-point decimal](https://stackoverflow.com/questions/79677/whats-the-best-way-to-do-fixed-point-math) instead of floating-point ones? – KaiserKatze Dec 29 '20 at 15:24
  • thanks guys for the info. actually the numbers recieved from the other system contain long fractions such as 3.1234 or 8.33333333 or 5.1666667. the problem is that the debit sum and credit sum are equal if adding the numbers as is without rounding. but once converted from double to decimal(18,2) sometimes the debit sum is not equal to the credit sum. a simple example is when a number is rounded up in debit and another number is rounded down in credit thus creating a difference of 0.01 or so... – microgen Dec 29 '20 at 17:35
  • @microgen "sometimes the debit sum is not equal to the credit sum." --> rather than only describe the problem, post code the demonstrates it - makes for a much better and solvable question. – chux - Reinstate Monica Dec 31 '20 at 06:22

0 Answers0