157

I have found an example which limits a number to 2 decimal places AND turns the figure into a currency amount- eg £2.55.

{{ number | currency : 'GBP' : true : '1.2-2'}}

Is there a simple pipe which does the same without applying a currency?

4 Answers 4

348

Currency pipe uses the number one internally for number formatting. So you can use it like this:

{{ number | number : '1.2-2'}}
7
  • 3
    refer for details regarding using number pipe
    – akhouri
    Commented Apr 11, 2017 at 0:39
  • 22
    This was helpful in understanding why 1.2-2 stackoverflow.com/questions/38477970/…
    – maudulus
    Commented May 23, 2017 at 2:16
  • 25
    For future readers, {{ x | number : '1.2'}} is also valid and means the same thing. Commented Jun 28, 2017 at 17:27
  • 14
    mehaase, is not the same. For example if you have 5.6 and 5.6789 the output will be 5.60 and 5.6789. So, the first parameter is the min and the second is the number of maximum digits. Commented Aug 11, 2017 at 11:15
  • 2
    How to use Pipe to transform a number to 1 decimal place and without roundup. Ex: 345.678 => 345.6
    – Brian
    Commented Dec 1, 2017 at 3:46
16

It's Works

.ts -> pi = 3.1415

.html -> {{ pi | number : '1.0-2' }}

Ouput -> 3.14
  1. if it has a decimal it only shows one
  2. if it has two decimals it shows both

https://stackblitz.com/edit/angular-e8g2pt?file=src/app/app.component.html

this works for me!!! thanks!!

10

Well now will be different after angular 5:

{{ number | currency :'GBP':'symbol':'1.2-2' }}
1
  • 1
    He wants to show value without currency symbol so DecimalPipe will work for him. Commented Feb 7, 2020 at 7:33
7

Simple solution

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

//output like this

// public orderTotal = 220.45892221

//   {{ orderTotal | number : '1.2-2'}} 

// final Output
//  220.45

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