New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect PercentPipe formatting #20136

Closed
pmatrosov opened this Issue Nov 3, 2017 · 6 comments

Comments

Projects
None yet
4 participants
@pmatrosov

pmatrosov commented Nov 3, 2017

I'm submitting a...


[X] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

PercentPipe sometime incorrectly interprets digitInfo's minFractionDigits parameter. It outputs text like minFractionDigits=maxFractionDigits.
{{ 0.12345674 | percent : '0.0-10' }} outputs 12.3456740000 % (this is error)
{{ 0.12345678 | percent : '0.0-10' }} outputs 12.345678 % (this is ok)

Expected behavior

{{ 0.12345674 | percent : '0.0-10' }} should output 12.345674 %
{{ 0.12345678 | percent : '0.0-10' }} should output 12.345678 %

DeprecatedPercentPipe works as expected.

Minimal reproduction of the problem with instructions

Create empty component and add above code to it

Environment


Angular version: 5.0.0


Browser:
- [X] Chrome (desktop) version 62.0.3202.75
- [X] Firefox version 56.0
- [X] IE version 11.0.9600.18816
 

@vicb vicb added the comp: i18n label Nov 3, 2017

@ocombe ocombe added the type: bug/fix label Nov 3, 2017

@ocombe

This comment has been minimized.

Show comment
Hide comment
@ocombe
Contributor

ocombe commented Nov 3, 2017

@ocombe

This comment has been minimized.

Show comment
Hide comment
@ocombe

ocombe Nov 10, 2017

Contributor

Ok so the problem comes from javascript, try to do 0.12345674*100 and you get 12.345673999999999 instead of 12.345674, not sure if we can fix this

Contributor

ocombe commented Nov 10, 2017

Ok so the problem comes from javascript, try to do 0.12345674*100 and you get 12.345673999999999 instead of 12.345674, not sure if we can fix this

@trotyl

This comment has been minimized.

Show comment
Hide comment
@trotyl

trotyl Nov 10, 2017

Contributor

Could it be fixed by using native Number#toFixed() method? Like:

image

Contributor

trotyl commented Nov 10, 2017

Could it be fixed by using native Number#toFixed() method? Like:

image

ocombe added a commit to ocombe/angular that referenced this issue Nov 10, 2017

@ocombe

This comment has been minimized.

Show comment
Hide comment
@ocombe

ocombe Nov 10, 2017

Contributor

I've used something similar that seems to do the trick:

function multiFloats(a: number, b: number){
  const aPow = Math.pow(10,String(a).length - String(a).indexOf(DECIMAL_SEP) - 1);
  const bPow = Math.pow(10,String(b).length - String(b).indexOf(DECIMAL_SEP) - 1);
  return (a * aPow) * (b * bPow) / (aPow * bPow);
}
Contributor

ocombe commented Nov 10, 2017

I've used something similar that seems to do the trick:

function multiFloats(a: number, b: number){
  const aPow = Math.pow(10,String(a).length - String(a).indexOf(DECIMAL_SEP) - 1);
  const bPow = Math.pow(10,String(b).length - String(b).indexOf(DECIMAL_SEP) - 1);
  return (a * aPow) * (b * bPow) / (aPow * bPow);
}

ocombe added a commit to ocombe/angular that referenced this issue Nov 10, 2017

@trotyl

This comment has been minimized.

Show comment
Hide comment
@trotyl

trotyl Nov 10, 2017

Contributor

That's awesome

Contributor

trotyl commented Nov 10, 2017

That's awesome

@vicb

This comment has been minimized.

Show comment
Hide comment
@vicb

vicb Dec 7, 2017

Contributor

What we discussed with @ocombe relative to

{{ 0.12345674 | percent : '0.0-10' }} should output 12.345674 %

in JS console.log(0.12345674 * 100) is 12.345673999999999

So the pipe result should be either 12.3456739999 or 12.345674 (not sure what we are supposed to do with rounding) but 12.3456740000 is clearly wrong.

@ocombe will fix this but he will not fix floating point number in JS.

Contributor

vicb commented Dec 7, 2017

What we discussed with @ocombe relative to

{{ 0.12345674 | percent : '0.0-10' }} should output 12.345674 %

in JS console.log(0.12345674 * 100) is 12.345673999999999

So the pipe result should be either 12.3456739999 or 12.345674 (not sure what we are supposed to do with rounding) but 12.3456740000 is clearly wrong.

@ocombe will fix this but he will not fix floating point number in JS.

ocombe added a commit to ocombe/angular that referenced this issue Dec 13, 2017

ocombe added a commit to ocombe/angular that referenced this issue Dec 13, 2017

ocombe added a commit to ocombe/angular that referenced this issue Dec 19, 2017

ocombe added a commit to ocombe/angular that referenced this issue Dec 20, 2017

ocombe added a commit to ocombe/angular that referenced this issue Dec 20, 2017

ocombe added a commit to ocombe/angular that referenced this issue Dec 20, 2017

ocombe added a commit to ocombe/angular that referenced this issue Dec 20, 2017

IgorMinar added a commit that referenced this issue Dec 22, 2017

@IgorMinar IgorMinar closed this in 07b81ae Dec 22, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment