Monetary units around the world are often expressed in terms of decimal numbers. You would think that by this time computers would be adept at handling such, but as this page indicates, sadly such is not the case for JavaScript today. This befuddles businessmen and causes application developers to focus attention on unnecessary details unrelated to solving the problem at hand.
One of my tasks is to write the spec text for future revisions of ECMAScript to address this by introducing a notion of a Decimal class. As currently envisioned, this will be accomplished in three layers:
The first layer provides a constructor for Decimal with a string argument, and a number of class ("static") methods modeled after the decFloats module in the decNumber library, and a bare minimum of instance methods, such as toString.
The second layer provides a number of convenience methods inspired by Java’s BigDecimal class.
The third layer introduces decimal literals (e.g. 1.2m, and no, I don’t know what m stands for) and infix operators.
One thing standards bodies often value is independent implementations. Accordingly, I plan to integrate the decNumber library into the Mozilla codebase. The decNumber library is made available under a very liberal license and will do all the heavy lifting, all I will need to focus on is a bit of glue code.
Since the last time I looked at the Mozilla codebase, it has moved from CVS to Mercurial. I’ve managed to check out both Mozilla central and a jsuni branch, and build firefox from source. The jsuni branch proports to reenable the ability to build a standalone js executable, but at the moment this does not appear to be the case.
If anybody has any pointers on how to build a standalone js executable and how to trigger the execution of the unit tests, I would appreciate it. I’m sure it is a simple matter of RTFM, if I can only find the right M.
m=money, I have always thought.
Whether that was the original intention I don’t know, but at the very least that definition might serve as a reminder to developers who keep trying to handle currency with floating point numbers. I’ve found code like that running live in financial transactions at more than one major bank.
being an enterprise software developer; and having used different strategies over the last years for handling money and currencies. I came to the conclusion that the best and most unambiguous way to handle monetary units is using integers.
A fool also did manage converting ICU’s BigDecimal implementation to JavaScript: [link] (this file depends on the other javascript file in the same directory).
It’s obvious that having a BigNumber native ECMAScript object would be beneficial to everyone, but in the mean time it seems that it’s possible to live without...
Probably for “build standalone” too, but certainly for any question involving “run,” "JS," and “tests” the answer is “you have Bob Clary’s email address, right?”
By a weird coincidence, I was just thinking about implementing decimal arithmetic in JavaScript, and about JavaScript unit testing.
There’s a project JsUnit which provides a port of JUnit to JavaScript, and it would probably also be worth making JSLint part of the testing.
Having worked on the Ruby BigDecimal library’s documentation, I didn’t see anything particularly wrong with its API, other than the need to refer to BigDecimal everywhere.
I don’t like the ‘m’ suffix. Presumably ‘d’ for decimal was considered too likely to confuse C and Java programmers. What about # for number?
Well, JSON is the new hotness . This week. Again. Thought I’d jot down some thoughts I had on JSON last summer, in the area of improving on JSON. Before doing that, let me go over some of my core beliefs about JSON: It defines an ASCII...
Well, JSON is the new hotness . This week. Again. Thought I’d jot down some thoughts I had on JSON last summer, in the area of improving on JSON. Before doing that, let me go over some of my core beliefs about JSON: It defines an ASCII...