Number pipes in Angular2 not working as I would expect

26,133

You're probably looking for the DecimalPipe.

The formatting is passed as a parameter to the pipe like this:

number:'{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}'

Using this with your needs:

number:'1.2-2'

This will give you a string with minimum 1 digit before decimal point and exactly 2 after decimal point.


Example usage from the docs:

@Component({
  selector: 'number-example',
  pipes: [DecimalPipe],
  template: `<div>
    <p>e (no formatting): {{e}}</p>
    <p>e (3.1-5): {{e | number:'3.1-5'}}</p>
    <p>pi (no formatting): {{pi}}</p>
    <p>pi (3.5-5): {{pi | number:'3.5-5'}}</p>
  </div>`
})
export class NumberPipeExample {
  pi: number = 3.141;
  e: number = 2.718281828459045;
}
Share:
26,133

Related videos on Youtube

Piddien
Author by

Piddien

Updated on July 09, 2022

Comments

  • Piddien
    Piddien almost 2 years
    platform-browser.umd.js:962 ORIGINAL EXCEPTION: 2 is not a valid digit info for number pipes
    

    Is what I get from writing this in my template:

    <li>Percentage satisfied: {{selectedCountry.averageSatisfaction | number:2}}</li>
    

    My intention is to produce a decimal number with only two decimals. Can someone please point out what I am doing wrong? Here is the documentation: https://docs.angularjs.org/api/ng/filter/number

Related