What are the parameters for the number Pipe - Angular 2

AngularPipeDecimal

Angular Problem Overview


I have used the number pipe below to limit numbers to two decimal places.

{{ exampleNumber | number : '1.2-2' }}

I was wondering what the logic behind '1.2-2' was? I have played around with these trying to achieve a pipe which filters to zero decimal places but to no avail.

Angular Solutions


Solution 1 - Angular

The parameter has this syntax:

{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}

So your example of '1.2-2' means:

  • A minimum of 1 digit will be shown before decimal point
  • It will show at least 2 digits after decimal point
  • But not more than 2 digits

Solution 2 - Angular

  1. Regarding your first question.The pipe works as follows:

numberValue | number: {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}

  • minIntegerDigits: Minimum number of integer digits to show before decimal point,set to 1by default

  • minFractionDigits: Minimum number of integer digits to show after the decimal point

  • maxFractionDigits: Maximum number of integer digits to show after the decimal point

2.Regarding your second question, Filter to zero decimal places as follows:

{{ numberValue | number: '1.0-0' }}

For further reading, checkout the following blog

Solution 3 - Angular

From the DOCS

> Formats a number as text. Group sizing and separator and other > locale-specific configurations are based on the active locale.

SYNTAX:

number_expression | number[:digitInfo[:locale]]

where expression is a number:

digitInfo is a string which has a following format:

{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
  • minIntegerDigits is the minimum number of integer digits to use.Defaults to 1
  • minFractionDigits is the minimum number of digits
  • after fraction. Defaults to 0. maxFractionDigits is the maximum number of digits after fraction. Defaults to 3.
  • locale is a string defining the locale to use (uses the current LOCALE_ID by default)

DEMO

Solution 4 - Angular

'1.0-0' will give you zero decimal places i.e. no decimals. e.g.$500

Solution 5 - Angular

'0.0-0' will give you round formatted number with ','

100000.2 -> 100,000

very cool

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionrushtoni88View Question on Stackoverflow
Solution 1 - AngularrinukkusuView Answer on Stackoverflow
Solution 2 - AngularMwizaView Answer on Stackoverflow
Solution 3 - AngularSajeetharanView Answer on Stackoverflow
Solution 4 - Angularalchi bauchaView Answer on Stackoverflow
Solution 5 - Angularyael kfirView Answer on Stackoverflow