Live data from Hacker News

How to Handle Monetary Values in JavaScript

frontstuff.io

21–30 of 115 posts

Re: How to Handle Monetary Values in JavaScript

#21

I looked into using Dinero.js before but it was overkill for my use case of calculating cart totals in USD only. I went with Big.js because it was better suited for what I wanted: a small library for decimal arithmetic.

I think the advantage behind using something like Big.js is to future proof the code against hyper-inflation. Though a dedicated currency engine would better have the ability to set and conveniently update the currency denomination of stored values to cope with this. With float64's maximum 'safe' integer at almost ten thousand trillion, I believe this should be enough to compute most commercial transactions securely…

This approach, at least in languages like C and Java, would effectively smush the effective MAX_INT down to MAX_INT/1e15 or whatever. No bueno.

Re: How to Handle Monetary Values in JavaScript

#22
post #5
post #3

Earlier quoted context omitted.

Can you illustrate a case where you can't use regular FP arithmetic and round to 2 decimal places at the end?

You can't model 1.0 correctly but it will be something like 1.000006667. If you add enough of these numbers you will be off.

Integers can be represented exactly in IEEE754.

Re: How to Handle Monetary Values in JavaScript

#24
> Floats: 0.1 + 0.2 // returns 0.30000000000000004

The question is how much of an issue is this really? How often is 4 parts in 10 quadrillion a meaningful error when dealing with money? Especially when most of the time JS is dealing with money it will be presentation.

Re: How to Handle Monetary Values in JavaScript

#26

Banking apps, e-commerce websites, stock exchange platforms, we interact with money daily. We also increasingly rely on technology to handle ours. Yet, there’s no consensus around how to programmatically handle monetary values. I wonder if the average person would find this frightening.

I don't think it's true that there's no consensus around how to programatically handle monetary values. Maybe there's no consensus around how to do it in JavaScript.

In the Java world, there's JSR-354: https://jcp.org/en/jsr/detail?id=354

Cobol has handled money well for decades.

.NET has Decimal, which should handle money correctly as long as you're working with values less than 79.2 octillion.

In my part of the world, at least, big banks tend to be mostly Java and Cobol on the back end. You'll definitely see Node, Go, and others being used for various tasks throughout the company, but when it comes to keeping track of money and moving it around, it's primarily Java and Cobol doing the heavy lifting.

That's what I've heard from other developers, anyway. I haven't worked at a bank myself.

Re: How to Handle Monetary Values in JavaScript

#28
post #8
post #6

Earlier quoted context omitted.

Ever seen the numeric tower of lisp? https://en.wikipedia.org/wiki/Numerical_tower The link to the scheme documentation that goes over this is quite good.

That's fantastic! Do you have any more resources on this? I'd like to look at that concept in more depth.

Nothing specific. The scheme manual linked is a good one. As are the docs for Common Lisp. https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node16.html

Honestly, most all older manuals and documents that were not related to systems programming seem to cover numeric programming decently enough. I get the impression that languages used to worry more about that sort of thing, but then the truly heavy lifting of numeric programming moved into the specialized domains of Matlab and friends, and then programmers slowly unlearned most of what was learned regarding numbers.

Re: How to Handle Monetary Values in JavaScript

#29
post #4

I really like the approach Perl 6 takes with FatRat[1]. You basically have an object which holds a numerator and denominator, so all arithmetic calculations do not loose precision. [1] https://docs.perl6.org/type/FatRat

Adding to the languages that do this, Factor does as well. "1 3 /" produces the rational value "1/3". https://docs.factorcode.org/content/article-rationals.html

Re: How to Handle Monetary Values in JavaScript

#30
post #12
post #3

Earlier quoted context omitted.

Can you illustrate a case where you can't use regular FP arithmetic and round to 2 decimal places at the end?

Bring up a Javascript interpreter and try this: $ node > 0.1 + 0.2 - 0.3 5.551115123125783e-17 Close enough for trivial cases but errors can accumulate. Ruby, Perl5, Python also fail this. Perl6 uses Rat numbers for things like this and doesn't have this problem.

> Ruby, Perl5, Python also fail this.

Ruby and Python have arbitrary precision decimal and rational support in the standard library. While there are arguments tobe had over whether using binary floats by default for simple decimal literals is a good choice, that's a different issue than not having any other kind of numbers available by default.

Post reply on HN