220

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.

4

5 Answers 5

395

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
4
  • 46
    This also unfortunately rounds the number, and even worse, there's no word about it in the documentation
    – phil294
    Commented Feb 12, 2017 at 19:55
  • 2
    @Blauhirn I have the same problem, was there an easy fix or should I create my own pipe?
    – S. Robijns
    Commented Apr 25, 2018 at 11:51
  • 9
    @phil294 I know your comment is old, but, now there is a description about rounding numbers. angular.io/api/common/DecimalPipe Commented Feb 22, 2020 at 20:36
  • If minFractionDigits is greater than 0, then getting error. The minimum number of digits after fraction (2) is higher than the maximum (0).' for pipe 'DecimalPipe'
    – cnsnaveen
    Commented May 21, 2021 at 8:15
24
  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

10

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

5

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

100000.2 -> 100,000

very cool

2

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

3
  • I am trying to format a number like 114.5 to display as 114 but when I use '1.0-0' it displays '115' anybody know why?
    – Karaja
    Commented Oct 10, 2018 at 14:18
  • 1
    It's because angular rounds the value and gives 115 for 114.5. Commented Oct 11, 2018 at 1:26
  • 2
    Why the downvotes? this works and answers the question of how to filter to zero decimal places. Thanks @alchibaucha solved my problem precisely.
    – S..
    Commented Jan 14, 2019 at 10:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.