globalizejs / globalize

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
https://globalizejs.com
MIT License
4.8k stars 603 forks source link

Format a number without truncating decimal digits #535

Open gcastre opened 9 years ago

gcastre commented 9 years ago

Hello,

I have an issue with the number and currency formatter. I have some numbers that can have many decimal digits (from 2 to 5 (and maybe even more in the future)).

How can I tell the formatter that I only want the number formatted without touching the decimal digits ?

Ex:

Globalize.formatNumber(0.00398)
Expected: 0,00398
Current: 0,004
Globalize.formatNumber(0.0039)
Expected: 0,0039
Current: 0,004
Globalize.formatNumber(1000.00398)
Expected: 1 000,00398
Current: 1 000,004
rxaviers commented 9 years ago

By using maximumFractionDigits: 21, the biggest value. Does that help? Would you make any improvements at https://github.com/jquery/globalize/blob/master/doc/api/number/number-formatter.md in order to make this information clear?

gcastre commented 9 years ago

Ok, thank you for the quick response. I thought there might be a way to do this without setting an arbitrary value.

rxaviers commented 9 years ago

Rounding is kinda enforced by CLDR patterns (the 0s and #s, spec) and no Globalize option disables it, instead it controls its length.

Technically it would be easy not to round (needs change at https://github.com/jquery/globalize/blob/master/src/number/format/integer-fraction-digits.js) and I am open to listen for proposals. But, note the same behavior is also found on Ecma-402.