{
  "id": 2153938,
  "title": "Money as a data type",
  "url": "https://urgent.news/2026/08/20/money-as-a-data-type",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-20T14:35:30.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/mashhadi/money-as-a-data-type-14pk"
  },
  "original_language": "en",
  "account": "Money is not a simple number, but rather a number, currency, scale, and rounding policy. Using floats for money is not ideal as they can lose precision and cause unexpected results. Instead, using decimals or integers can provide more accurate calculations. However, these solutions still fall short of addressing the bug that most likely reaches production.\n\nThis bug involves adding monetary values of different currencies, resulting in an incorrect output of zero. The issue lies in the fact that money is not just a number, but also includes other factors like currency, scale, and rounding policy. If a type only carries the first factor (the number), the other three factors (currency, scale, and rounding policy) get scattered across call sites as assumptions.\n\nAnother problem arises when dealing with rounding. For example, when rounding to the nearest cent, a double may not accurately represent the desired value. In this case, the nearest double to 1.005 is 1.00499999999999989342, and multiplying by 100 gives 100.49999999999999. Rounding down gives the arithmetically correct result.\n\nWhen using decimals or integers, the arithmetic complaints disappear. However, the issue of missing currency, scale policy, and rounding mode remains. To address this, it is recommended to store minor units (cents, satang, fils) as integer values in a BigInt. This ensures exactness, no rounding at rest, and no near ceiling issues.\n\nTo display monetary values, hand the integer value through a string and use the Intl.NumberFormat to render the value accurately. This way, the currency's exponent is taken into account, and the value is rendered correctly, including decimal places when necessary. Rounding can also be handled as a policy decision, with half-even (banker's rounding) being the default for many use cases.",
  "summary": "Most guides open with 0.1 + 0.2 === 0.30000000000000004 and conclude \"don't use floats for money.\" True, and not very useful. The interesting question is what you replace it with, because \"use decimals\" and \"use integers\" are different answers that fail in different places. Neither of them addresses the bug most likely to reach production: a function that cheerfully adds 500 US dollars to 500…",
  "key_points": [
    "Money is a combination of number, currency, scale, and rounding policy",
    "Floats for money can lose precision and cause unexpected results",
    "Storing minor units as integer values in BigInt ensures exactness"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}