JavaScript: Decimal Values

17,777

Solution 1

It is often recommended1 to handle money as an integer representing the number of cents: 2572 cents instead of 25.72 dollars. This is to avoid the problems with floating-point arithmetic that you mention. Fortunately integer arithmetic in floating point is exact, so decimal representation errors can be avoided by scaling.


1Douglas Crockford: JavaScript: The Good Parts: Appendix A - Awful Parts (page 105).

Solution 2

Javascript does have floating point support. But anyway, for financial records, the simplest implementation would simply be storing your values in standard integers. You may either declare an integer to represent the amount in cents, or two integers one for dollars and one for cents.

So $18.57 would become 1857cents in the first technique or 18 dollars and 57 cents in the second.

This has the added advantage of being completely accurate, as integers are stored purely as unique binary representation, there would be no such thing as rounding errors.

Solution 3

Take a look at BigNumber and that post too.

Solution 4

It seems the following library implements decimal in js (node and browser): https://npmjs.org/package/jsdecimal

Share:
17,777

Related videos on Youtube

Mr. X
Author by

Mr. X

Updated on April 28, 2022

Comments

  • Mr. X
    Mr. X about 2 years

    What can I use as a decimal type in JavaScript? It's not supported (0.1 + 0.2 !== 0.3), and I need it for representing exact values in a banking/financial application. See The State and Future of JavaScript for a good read and the dirty details behind JavaScript and it's (lack of) support for decimal arithmetic.

    By "decimal", I mean either:

    1. infinite-range and arbitrary-precision (like BigDecimal in Java), or
    2. limited range and precision, but suitable for financial calculations (like decimal in C#).

    So, what library or solution exists for working with decimal values? Thanks!

    • s.m.
      s.m. almost 7 years
      @TimAbell fixed, now the link points to the video on YT.
  • Mr. X
    Mr. X over 13 years
    Yeah, JavaScript has binary floating point, which is a bad choice for 99% of the applications that use JavaScript. Do any JavaScript libraries exist that abstract the concept of decimal for me?
  • Razor Storm
    Razor Storm over 13 years
    Wouldn't it be simpler to just store it as integer like my example? All decimal implementations would have inherent inaccuracies due to how the architecture represents floating points. Don't worry about overflow, even the US national debt would fit neatly in a long type 64bit int.
  • adamJLev
    adamJLev over 13 years
    Just looking at that code makes me wanna cry. Interesting idea though, but it sucks to not be able to use regular operators + - / * etc
  • Mr. X
    Mr. X over 13 years
    How many bits are available in the integer part of JavaScript floating point? I might be able to squeeze it all in.
  • ysth
    ysth over 13 years
    @Mr. X: almost certainly 53 or more.
  • Mr. X
    Mr. X over 13 years
    The integer part of JavaScript's floating point is less than 64 bits.
  • Daniel Vassallo
    Daniel Vassallo over 13 years
  • T.J. Crowder
    T.J. Crowder almost 13 years
    @Razor: "All decimal implementations would have inherent inaccuracies due to how the architecture represents floating points." Not all. A true "decimal" implementation could (and would probably have to) do what Java's BigDecimal class does: Actually store the data as a string of digits, the way we write and think about it, plus a scale indicator so it knows where the decimal point is. For money stuff in the realm of figures most people are likely to deal with, storing numbers of cents is fine; but for a general-purpose "decimal" type, that's the kind of thing that would be required...
  • John
    John over 10 years
    @Crowder x86 architecture can work with floating-point decimals (one nibble per decimal-digit) for ages.
  • Kelly Elton
    Kelly Elton almost 6 years
    This is really good advice regardless of the programming language.