Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a Money class for JavaScript?
    primarykey
    data
    text
    <p><em>Disclaimer: complete rewrite for clarity as of 10/14/2011</em></p> <p><strong>Given</strong> the <code>number</code> primitive in JavaScript is an <a href="http://en.wikipedia.org/wiki/IEEE_754-2008" rel="nofollow noreferrer">IEEE 754</a> 64-bit floating point (<em>known in other languages as a double</em>), and <a href="https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency">using floats to model currencies is a <strong>bad idea</strong></a>, <strong>is a Money prototype</strong> (JavaScript) <strong>or a <a href="http://rzrsharp.net/2011/06/21/classes-in-coffeescript.html" rel="nofollow noreferrer">Coffeescript Class</a></strong> that eases use of pseudo-integer cents and string <a href="http://en.wikipedia.org/wiki/ISO_4217" rel="nofollow noreferrer">currency ISO 4217 code</a> to represent currency <strong>available</strong>? </p> <p>^ There's still gotta be a better way to say that.</p> <p>I'm hoping to find something that mirrors the common design pattern of the many other languages out there that do include an integer primitive.</p> <p>As examples, I'm familiar with the <a href="http://rubygems.org/gems/money" rel="nofollow noreferrer">money gem</a> for ruby, and the <a href="http://pypi.python.org/pypi/python-money/0.5" rel="nofollow noreferrer">python-money</a> package, both of which implement variations of this design pattern.</p> <p>Ideally looking for something that will play nice with <a href="http://documentcloud.github.com/backbone/" rel="nofollow noreferrer">backbone.js</a> and <a href="http://nodejs.org/" rel="nofollow noreferrer">node.js</a>, but all suggestions appreciated.</p> <p><em>Edit 4</em>: As far as I can tell, as long as an implementation of <code>roundDownOrUp ? floor : ceiling</code> is called on the Number after every operation (&amp; in between chained operations) everything would function as if one were dealing with integers.</p> <hr> <h3>Old information, retained to document the history of the question.</h3> <p>I read <a href="https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript">How can I format numbers as money in JavaScript?</a> where I found <a href="http://josscrowcroft.github.com/accounting.js/" rel="nofollow noreferrer">accounting.js</a> and <a href="http://wiki.jqueryui.com/w/page/39118647/Globalize" rel="nofollow noreferrer">jQuery Globalize</a> which both do pretty printing but are not designed to model currencies and perform operations with them.</p> <p><em>Edit 1</em>: Just found <a href="http://jsorm.com/wiki/Currency" rel="nofollow noreferrer">JSorm Currency</a> in the <a href="http://search.npmjs.org/#/jsorm-i18n" rel="nofollow noreferrer">npm registry</a> which is ISO 4217 aware, but does not appear to include any fixes for float "<em>gotchas</em>". Please correct if I have misread.</p> <p><em>Edit 2 folded into rewrite.</em></p> <p><em>Edit 3</em>: It looks like a good option would be to use <a href="https://github.com/substack/node-bigint" rel="nofollow noreferrer">node-bigint</a> as suggested by @RicardoTomasi.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload