rushtoni88
rushtoni88

Reputation: 4665

What are the parameters for the number Pipe - Angular 2

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.

Upvotes: 224

Views: 214428

Answers (5)

yael kfir
yael kfir

Reputation: 183

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

100000.2 -> 100,000

very cool

Upvotes: 5

Mwiza
Mwiza

Reputation: 8951

  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

Upvotes: 27

Maximilian Riegler
Maximilian Riegler

Reputation: 23506

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

Upvotes: 405

alchi baucha
alchi baucha

Reputation: 1040

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

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222522

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

Upvotes: 11

Related Questions