4 responses to “Coding Money in Gnucash for Android”

  1. Steven

    I hope I don’t come off as rude or rubbing it in but…

    You NEVER EVER EVER use a float or double when dealing with money in ANY language! NEVER!

    Now a bit more advice (that might cause you to refactor again). While BigDecimal is pretty good for money calculations it does have it’s problems. Division that produces a recurring decimal will throw an ArithmeticException unless you supply a rounding behavior and scale (scale is the number of digits to the right of the decimal).

    There are generally two approaches to dealing with money either track using a class like BigDecimal (if available in the language you are working with) or using long values stored as a number of fractions of a cent. In either case you need to know ahead of time how many decimal values you are tracking. Since this is a Gnucash android app I would recommend finding out what precision Gnucash uses. There are also specific rounding rules in certain currencies (such as currencies where the smallest value is .05 rather than .01).

    There is a specific (and rather simple) rounding algorithm that basically involves multiplying the number, truncating, and then diving again, but I don’t remember it off the top of my head. Google might help.

  2. Rudd-O

    Default GnuCash has the same behavior. Very irritating — makes it useless to account for Bitcoin.

  3. kiers

    Wow, i never expected computers can be SO convoluted in handling basic numeric calculation! I was using jabp lite in android (also written in java) and found data corruption and crashes in the android version and have now started using gnu. I thought even most basic calculators with cheap processors built in had MASTERED simple calculation, but no.

Leave a Reply