I’m happy to announce that
the decimal proposal—a proposed extension of JavaScript to support decimal numbers—is
now available as
an NPM package
called proposal-decimal
!
(Actually, it has been available for some time, made available not
long after we decided to pursue IEEE 754 Decimal128 as a data
model for the decimal proposal rather than some alternatives.
The old package was—and still is—available under a different
name—decimal128
—but I’ll be sunsetting that package in favor of the new one
announced here. If you’ve been using decimal128, you can continue
to use it, but you’ll probably want to switch to
proposal-decimal
.)
To use proposal-decimal
in your project, install the NPM
package. If you’re looking to use this code in Node.js or other JS
engines that support ESM, you'll want to import the code like this:
import { Decimal128 } from 'proposal-decimal'; const x = new Decimal128("0.1"); // etc.
For use in a browser, the file
dist/Decimal128.mjs
contains the
Decimal128
class and all its internal dependencies in
a single file. Use it like this:
<script type="module">
import { Decimal128 } from 'path/to/Decimal128.mjs';
const x = new Decimal128("0.1");
// keep rocking decimals!
</script>
The intention of this polyfill is to track the spec text for the decimal proposal. I cannot recommend this package for production use just yet, but it is usable and I’d love to hear any experience reports you may have. We’re aiming to be as faithful as possible to the spec, so we don’t aim to be blazingly fast. That said, please do report any wild deviations in performance compared to other decimal libraries for JS as an issue. Any crashes or incorrect results should likewise be reported as an issue.
Enjoy!
Jesse Alama