‹ BackHN Continuity

Thread

Adding Floating-Point Decimals for Fun and Profit

62 points · 35 comments · ibobev

  1. jgalt212 · · focus · HN ↗
    > Many people know that you shouldn’t do decimal calculations, such as those involving U.S. dollars and cents, with the floating-point numbers in most programming languages. This is because decimal numbers can’t be expressed exactly as such floating-point numbers, so you will encounter rounding errors.

    Decimals or pennies, you still have rounding errors. For example, if your contract is $100 / year. You will get the full $100 if you bill yearly, quarterly, or semi-annually. But if you bill monthly, no matter if you deal in pennies or fractional dollars, the sum total of your invoices for the year will be less than $100.

    1. gottheUIblues · · focus · HN ↗
      Bill 8 months at $8.33 and 4 months at $8.34?
      1. mytailorisrich · · focus · HN ↗
        Having 11 equal payments and a 1st or 12th one of a different amount to make the total exact is a fairly common way of dealing with this.
      2. jgalt212 · · focus · HN ↗
        Yeah, I know there's more than one way to skin a cat. My point is that storing all accounting values in pennies is overrated.
      3. astrobe_ · · focus · HN ↗
        If you're going for ad hoc solution to that specific problem, just charge $99/year or $102/year instead, depending on how you feel like about profit margins.
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.