Help Angular by taking a 1 minute survey!Go to surveyHome

DecimalPipe

Transforms a number into a string, formatted according to locale rules that determine group sizing and separator, decimal-point character, and other locale-specific configurations.

See more...

{{ value_expression | number [ : digitsInfo [ : locale ] ] }}
      
      {{ value_expression | number [ : digitsInfo [ : locale ] ] }}
    

NgModule

Input value

value any

The number to be formatted.

Parameters

digitsInfo string

Decimal representation options, specified by a string in the following format:
{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}.

  • minIntegerDigits: The minimum number of integer digits before the decimal point. Default is 1.
  • minFractionDigits: The minimum number of digits after the decimal point. Default is 0.
  • maxFractionDigits: The maximum number of digits after the decimal point. Default is 3.

Optional. Default is undefined.

locale string

A locale code for the locale format rules to use. When not supplied, uses the value of LOCALE_ID, which is en-US by default. See Setting your app locale.

Optional. Default is undefined.

See also

Description

If no parameters are specified, the function rounds off to the nearest value using this rounding method. The behavior differs from that of the JavaScript Math.round() function. In the following case for example, the pipe rounds down where Math.round() rounds up:

-2.5 | number:'1.0-0' > -3 Math.round(-2.5) > -2
      
      -2.5 | number:'1.0-0'
> -3
Math.round(-2.5)
> -2
    

Usage notes

The following code shows how the pipe transforms numbers into text strings, according to various format specifications, where the caller's default locale is en-US.

Example

@Component({ selector: 'number-pipe', template: `<div> <!--output '2.718'--> <p>e (no formatting): {{e | number}}</p> <!--output '002.71828'--> <p>e (3.1-5): {{e | number:'3.1-5'}}</p> <!--output '0,002.71828'--> <p>e (4.5-5): {{e | number:'4.5-5'}}</p> <!--output '0 002,71828'--> <p>e (french): {{e | number:'4.5-5':'fr'}}</p> <!--output '3.14'--> <p>pi (no formatting): {{pi | number}}</p> <!--output '003.14'--> <p>pi (3.1-5): {{pi | number:'3.1-5'}}</p> <!--output '003.14000'--> <p>pi (3.5-5): {{pi | number:'3.5-5'}}</p> <!--output '-3' / unlike '-2' by Math.round()--> <p>-2.5 (1.0-0): {{-2.5 | number:'1.0-0'}}</p> </div>` }) export class NumberPipeComponent { pi: number = 3.14; e: number = 2.718281828459045; }
      
      
  1. @Component({
  2. selector: 'number-pipe',
  3. template: `<div>
  4. <!--output '2.718'-->
  5. <p>e (no formatting): {{e | number}}</p>
  6. <!--output '002.71828'-->
  7. <p>e (3.1-5): {{e | number:'3.1-5'}}</p>
  8.  
  9. <!--output '0,002.71828'-->
  10. <p>e (4.5-5): {{e | number:'4.5-5'}}</p>
  11. <!--output '0 002,71828'-->
  12. <p>e (french): {{e | number:'4.5-5':'fr'}}</p>
  13.  
  14. <!--output '3.14'-->
  15. <p>pi (no formatting): {{pi | number}}</p>
  16. <!--output '003.14'-->
  17. <p>pi (3.1-5): {{pi | number:'3.1-5'}}</p>
  18.  
  19. <!--output '003.14000'-->
  20. <p>pi (3.5-5): {{pi | number:'3.5-5'}}</p>
  21.  
  22. <!--output '-3' / unlike '-2' by Math.round()-->
  23. <p>-2.5 (1.0-0): {{-2.5 | number:'1.0-0'}}</p>
  24. </div>`
  25. })
  26. export class NumberPipeComponent {
  27. pi: number = 3.14;
  28. e: number = 2.718281828459045;
  29. }