question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for different precision currencies

See original GitHub issue

Not all currencies have two digits after the decimals. Some have 4 digits after the decimal, some have 0 digits after the decimal, while the majority have 2 digits after the decimal.

It would be nice to have an option for the constructor top specify the precision either as a method, or during creation:

currency(2.51)  // 2.51
currency(2.51, 4)  // 2.5100, with precision option

or

currency.settings.precision(4)

Although this would change the “global” settings for all currency instances after required. It would be nice to set the formatting, decimal, separator, symbol and precision on a per usage basis (I have a site that needs to work in multiple currency formats in NodeJS, and altering the settings currently affects everything)

Many suggest that currencies be stored with 4 digits after the decimal in databases (along with the currency code in use), so maybe switching from 2 digits in the internal native format to 4 digits might also be an option.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
scurkercommented, Jul 23, 2017

@tmorehouse A long wait, but good news! I have a branch where precision is working. There’s a few breaking changes, which I intend to release as v1.0.0 soon.

currency.settings is now no longer global. Due to the nature of having multiple currencies running in parallel, it did not make sense to have a global magic settings for all. Unfortunately, if you depend on those global settings you will need to make a few changes before you can try out the latest.

Settings are now passed to the currency object as a second parameter, including precision! This allows you to define multiple currencies with whatever precision you want.

const USD = value => currency(value, { symbol: '$', precision: 2 });
const JPY = value => currency(value, { symbol: '¥', precision: 0 });
const GAS = value => currency(value, { precision: 3 });

I’m still testing changes locally to ensure that I didn’t miss anything significant, but if you have a moment feel free to check out that branch and see if it works for you.

0reactions
tmorehousecommented, Jul 26, 2017

OK… I’ll open a separate issue regarding .valueOf()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Decimal precision in currency and pricing (Dynamics 365 Sales)
To see the field properties, go to Settings > Customizations > Entity > Fields. Open the currency field to see the properties.
Read more >
Setting Form Precision and Other Options - Oracle Help Center
Value Minimum Precision Maximum Precision 100 0 Any 100 3 Any number greater than or equal to 3 or No... 100.12345 Any number less than or...
Read more >
Currency codes and minor units - Adyen Docs
Code Currency Decimals AED UAE Dirham 2 ALL Albanian Lek 2 AMD Armenian Dram 2
Read more >
money-lib - PyPI
Python 3 money lib with decimal precision and currency exchange support. ... pip install money-lib ... Money supports formatting for different locales.
Read more >
Using BigDecimal to work with currencies - Stack Overflow
Use BigDecimal for computations if you need the precision that it offers (Money values ... Maybe in the future MongoDb will add support...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found