29

Is there a mature library for doing decimal-based math, possibly arbitrary-precision, in JavaScript?

Edit: I want this information for a reference page on floating-point-related problems and alternatives to use when binary floating-point is inappropriate: http://floating-point-gui.de/

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 'The Server'. Seriously, wherefore do you need it? Serverside JS? Some special math? 3D on a canvas? ... That could perhaps help to search in the right direction. – Boldewyn Apr 12 '10 at 12:57
  • 3
    Good question, but a duplicate. See: http://stackoverflow.com/questions/287744/good-open-source-javascript-math-library-for-floating-point-operations – Anthony Apr 12 '10 at 12:58
  • 5
    @Anthony that question is about **floating point**, while this question is (I think) about infinite-precision fixed-point. The second one you linked is clearly irrelevant. – Pointy Apr 12 '10 at 12:59
  • 1
    There's another similar question: http://stackoverflow.com/questions/744099/javascript-bigdecimal-library, but it is a year old. I'm curious on any updates. – Daniel Vassallo Apr 12 '10 at 12:59
  • @Boldewyn common use case: monetary manipulation for spreadsheet-like user interfaces – Pointy Apr 12 '10 at 13:03
  • I was looking for such some time ago and I decided to write very simple class for my own (30 lines). I use integers + shifting for internal math so it has limited precision but for money calculations it works fine. – Lukasz Korzybski Sep 10 '10 at 15:08

3 Answers3

19

As their author, I recommend bignumber.js or big.js, 'a small, fast Javascript library for arbitrary-precision arithmetic with decimal numbers'.

For a more mature library, the ICU4J BigDecimal translation is also recommended.

MikeM
  • 13,156
  • 2
  • 34
  • 47
6

There's been a "port" of the Java BigDecimal class (I think it's here: http://freshmeat.net/projects/js_bigdecimal/ ) for a long time. I looked at it a long time ago and it seemed kind-of cumbersome and huge, but (if that's the one I'm thinking of) it's been used as part of some cryptography tools so there's a decent chance that it works OK.

Because cryptography is a likely area to generate a need for such things, that's a good way to snoop around for such packages.

edit: Thanks @Daniel (comment to question) for this older SO question: https://stackoverflow.com/questions/744099/javascript-bigdecimal-library

Community
  • 1
  • 1
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • The official homepage of the BigDecimal port seems to be this: http://stz-ida.de/html/oss/js_bigdecimal.html.en - I couldn't even find a way to download it from freshmeat. – Michael Borgwardt Apr 19 '10 at 09:16
  • Ah. Well it shows up all over the place in google searches. It's been around for a long time. – Pointy Apr 19 '10 at 13:26
  • 1
    The BigDecimal link is getting 404. Seems the site was redesigned. New link: http://stz-ida.de/index.php?option=com_content&view=article&id=18&Itemid=32 – spoulson Aug 31 '10 at 14:12
  • 1
    @spoulson, that new link gives a 404 also... :S – Abel Oct 28 '11 at 12:13
1

Since it does not seem to be mentioned, here's another one. I haven't used it myself (yet), so I will update the post once I've given it a try.