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 602 forks source link

Rounding with significant digits precision gives wrong results #821

Closed shivijais closed 6 years ago

shivijais commented 6 years ago
Globalize.numberFormatter({
        compact: 'short',
        minimumSignificantDigits: 1,
        maximumSignificantDigits: 3,
    })(12849872883)

Actual => 12.9B Expected => 12.8B

Root cause for this is in the method numberToPrecision which first changes the precision from actual precision to a +2. and then rounds the number based on the given precision. So the number in this case 12849872883 changes to 12.850 and when finally rounded becomes 12.9

rxaviers commented 6 years ago

Thanks for reporting and for the initial investigation. Fix would be welcome :). Thanks