It’s just data

Decimal in ECMAScript

10

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:

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.

Posted by Ciaran at

Why not use mozilla’s rhino? [link]

Posted by Rob Koberg at

irb(main):001:0> puts 1.2 - 1.1
0.0999999999999999
=> nil
irb(main):002:0>

Posted by Phil Wilson at

Rhino is a possibility too.

irb(main):001:0> require 'bigdecimal'
=> true
irb(main):002:0> puts (BigDecimal('1.2')-BigDecimal('1.1')).to_s('F')
0.1
=> nil
Posted by Sam Ruby at

Have a look at [link] but having it included in the language would be a good thing.

Posted by Thomas Broyer at

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.

Posted by Slim Amamou at

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...

Posted by Thomas Broyer at

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?”

Posted by Phil Ringnalda at

Looks like it shouldn’t be too hard to guess.  Thanks for the pointer!  I’ll follow up.

Posted by Sam Ruby at

rubys@rubix:~/hg/js-decimal/js/src$ make -f Makefile.ref
...
make[1]: Leaving directory `/home/rubys/hg/js-decimal/js/src'
rubys@rubix:~/hg/js-decimal/js/src$ Linux_All_DBG.OBJ/js -v
JavaScript-C 1.8.0 pre-release 1 2007-10-03
usage: js [-zKPswWxCi] [-b branchlimit] [-c stackchunksize] [-o option] [-v version] [-f scriptfile] [-e script] [-S maxstacksize] [-Z gczeal] [scriptfile] [scriptarg...]
rubys@rubix:~/hg/js-decimal/js/src$ Linux_All_DBG.OBJ/js
js>
Posted by Sam Ruby

at

With regard to the Ruby decimal subtraction, I’m with Shania Twain. That don’t impress me much.

Posted by Phil Wilson at

The test suite for Python’s decimal module may be useful to you in helping build an extensive JS test suite.

Posted by amk at

PEP 327: test_decimal.py

Posted by Sam Ruby at

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?

Posted by mathew at

better than json

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...

Excerpt from pmuellr at

better than json

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...

Excerpt from pmuellr at

Sam Ruby: Decimal in ECMAScript

Sam Ruby: Decimal in ECMAScript hnakamur3 javascript , decimal [link]...

Excerpt from hnakamur3 のブックマーク at

Add your comment