Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Why don't applications typically use [whole numbers] to internally represent currency values?</strong></p> <ol> <li><p>It does not make for simple coding. $1.10 translates to 110¢. Okay, but what about when you need to calculate tax (i.e $1.10 * 4.225% - Missouri's tax rate which results in $0.046475). To keep all money in whole numbers you'd have to also convert the sales tax to a whole number (4225), which would require converting 110¢ further to 11000000. The math then can be 11000000 * 4225 / 100000 = 464750. This is a problem as now we have values in fractions of cents (11000000 and 464750 respectively). All this for the sake of storing money as whole numbers.</p></li> <li><p>Therefore, it's easier to think and code in terms of the native currency. In the United States, this would be in dollars with the cents being a decimal fraction (i.e. $1.10). Coding such in terms of 110¢ isn't as natural. Using base-10 floating point numbers (such as Java's <code>BigDecimal</code>, and .NET's <code>Decimal</code>) are usually precise enough for currency values (compared to base-2 floating point numbers like <code>Float</code> and <code>Double</code>).</p></li> </ol> <p><strong>Why isn't this approach a best practice among applications that don't need to represent fractions of cents (or the equivalent in other currency types)?</strong></p> <p>I think number 1 above shows that it's hard to get away from needing to represent fractions of cents, at least when it comes to calculating sales tax - something common in business applications.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. CO+1 because it talks about the fact that "bare" integer don't built-in have support for many required operations. '$1.10' is display-currency though. Mils are 1/1000$. Also, `BigDecimal` is *exact* (for a certain subset of operations / desired memory expenditure and offers customizable rounding modes) while `decimal` (.NET) is just a *really huge 128-bit relative precision number* that can just brute-force past a number of issues but still "suffers" from not being a fixed precision :)
      singulars
      1. This table or related slice is empty.
    2. COMatt, can you clarify your point regarding "To keep all money in whole numbers"? I would represent tax rate as a floating-point value (as tax rate is not a currency value); then I would multiply my integer currency value by the double floating-point value to find my result, and accept any loss of precision as being fractions of pennies which I can discard. Assuming we do not have a requirement to track fractions of pennies for the purposes of sales tax to be remitted to the state -- might be an issue, but assume there are viable workarounds -- is there any other downside to this approach?
      singulars
    3. CO@Jon, I was just trying to give an example of how it might not always be simple and the things you would have to go through to not ever loose precision (if that is desired since using floats is commonly discouraged due to loose of precision). However, doing intermediate math with floats makes sense for sales tax calculations, and would probably be more common.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload