Scientific notation support
ghtaylor opened this issue · 1 comments
Expected Behavior
- Provide a string
value
that uses scientific notation tocurrency(value)
- Value represented in
currency
object is correct
Actual Behavior
- Provide a string
value
that uses scientific notation tocurrency(value)
- Value represented in
currency
object is incorrect
Steps to Reproduce the Problem
CodeSandbox describing problem
- Declare string that is a currency amount represented by scientific notation:
const scientificNotationAmount: string = "1.299078055E7";
- Provide string as
value
tocurrency(value)
const amount = currency(scientificNotationAmount);
- Log
.value
property ofamount
// Currency.js value
console.log(amount.value) // 1.3
// Expected value
console.log(Number(scientificNotationAmount)) // 12_990_780.55
Motivation
Whilst integrating with a third-party accounting and finance management system, their APIs return amount values over 10,000,000 or less than -10,000,000 as scientific notation values. We did not know that currency.js
doesn't pick this up, and it caused a bug in our system.
Note
I am happy to try and make a PR if its agreed this would be a worthwhile enhancement? Please let me know.
I'm not sure how common this is, having currency values formatted as scientific notation isn't something that I've commonly seen. Ideally currency.js is intended to be a lightweight currency formatter with a lot of bloat, so I'm not inclined to include a feature that's not commonly used.
My short suggestion would be if you know a value is in scientific notation, is to throw the value into Number
and let currency do the rest. It should be safe for numeric values as well:
currency(Number('1.299078055E7'))