openexchangerates / accounting.js

A lightweight JavaScript library for number, money and currency formatting - fully localisable, zero dependencies.
http://openexchangerates.github.io/accounting.js
MIT License
4.96k stars 530 forks source link

Optional / Ranged precision #142

Open dougajmcdonald opened 8 years ago

dougajmcdonald commented 8 years ago

Using accounting.js is there a way to allow the formatter to handle the following cases:

I enter 5 and it formats it with 0 dp precision - output 5 I enter 5.1 and it formats with 1 dp precision - output 5.1 I enter 5.25 and it formats with 2dp precision - output 5.25 I enter 5.254 and it formats to 2dp precision - output 5.25 In short I want the precision to match the number of characters after the dp but only up to 2. So a range of 0-2 dp precision if that makes sense!

I would like the precision to be optional.

Posted on SO here too: http://stackoverflow.com/questions/36594706/condition-precision-or-precision-range-in-accounting-js

rofrol commented 7 years ago
function currencyUSD(number) {
  return number.toLocaleString('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 0 });
}
currencyUSD(123456); // "$123,456"
currencyUSD(123456.234); // "$123,456.23"