scurker / currency.js

A javascript library for handling currencies
https://currency.js.org
MIT License
3.16k stars 142 forks source link

Support for BigInt? #391

Open shaunhurley opened 2 years ago

shaunhurley commented 2 years ago

Hey folks, curious to understand what (if any) plans you have for supporting the bigint type?

I'm migrating some code to use the latest Square SDK and noticed that they are now returning all numbers as bigint, and creating a currency from a bigint results in a $0.00 value.

currency(100).format() --> $1.00 currency(100n).format() --> $0.00

scurker commented 2 years ago

When this library was first created, BigInt wasn't widely supported across all browsers - but that has certainly changed.

There's some considerations to be made because we'd either have to support BigInt internally, or truncate to the max integer, which negates some of the benefits from using BigInt. I would certainly thing supporting this in the future could be a possibility though.

shaunhurley commented 2 years ago

I figured as much, and it's not even that there is necessarily a need to work with numbers that big, just that it's becoming more common for source data to be returned in that (bigInt) format - Square (my case in question) is very widely used, for example.

In the short term, since bigInt's stringify to a usable integer (within the bounds of discussions), I am able to resolve my issues by making my current calls with a .toString() on the end of the values. That might be sufficient as a short term (non-breaking) fix for the interim.