Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Number Decimal Separator Incorrect for exponentially small numbers #10342

Closed
evanliomain opened this issue Dec 5, 2014 · 4 comments
Closed

Comments

@evanliomain
Copy link

Issue

The number decimal separator are incorrect for the fr-fr locale in formatNumber for exponentially small numbers.

Expected

  • Decimal: ,

Actual

  • Decimal: .

In fact, it's an issue for all languages that do not have a dot as decimal separator.

I suggest the same use case than "should filter exponentially small numbers" to test this issue:

     it('should filter exponentially small numbers on non dot decimal separator languages', function() {
       $locale.NUMBER_FORMATS.DECIMAL_SEP = ',';
       expect(number(1e-50, 0)).toEqual('0');
       expect(number(1e-6, 6)).toEqual('0,000001');
       expect(number(1e-7, 6)).toEqual('0,000000');

       expect(number(-1e-50, 0)).toEqual('0');
       expect(number(-1e-6, 6)).toEqual('-0,000001');
       expect(number(-1e-7, 6)).toEqual('-0,000000');
     });

Solve suggestion

I figure out where the issue is localized: https://github.com/angular/angular.js/blob/g3_v1_3/src/ng/filter/filters.js#L206

    if (fractionSize > 0 && number < 1) {
      formatedText = number.toFixed(fractionSize);
      number = parseFloat(formatedText);
    }

The .toFixed method is directly use to render the final number. And so, does NOT care about i18n at all.

To solve this issue, I suggest just to replace the dot by the expected decimal separator. As we deal this just number < 1, we don't have to manage thousand group separators.

    if (fractionSize > 0 && number < 1) {
      formatedText = number.toFixed(fractionSize);
      number = parseFloat(formatedText);
      formatedText = formatedText.replace(DECIMAL_SEP, decimalSep);
    }
@pkozlowski-opensource
Copy link
Member

@evanliomain yes, this is a valid bug. Currently we've got a code-path that calls number.toFixed(fractionSize) to format exponentially small numbers. This is wrong as this code-path completly ignores locale-specific settings.

Sadly the current implementation needs a bit of love to simplify it.

@marcin-wosinek
Copy link
Contributor

Plunker demonstrating issue - http://plnkr.co/edit/vwuFsuRVWOjK4i3bsUpj?p=preview

@Heddy147
Copy link

I have a similar problem.
There is a validation in a form with a field with type number.
See my Plunker demonstration:
http://plnkr.co/edit/OH8rQSkItepd7sEDVYeM?p=preview

I get an error if I type a number smaller than 0.000001

@petebacondarwin
Copy link
Contributor

I have a fix for this if anyone cares to review: #12850

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants