Limit to 2 decimal places with a simple pipe

AngularPipeDecimal

Angular Problem Overview


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?

Angular Solutions


Solution 1 - Angular

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

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

Solution 2 - Angular

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!!

Solution 3 - Angular

Well now will be different after angular 5:

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

Solution 4 - Angular

> Simple solution

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

//output like this

// public orderTotal = 220.45892221

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

// final Output
//  220.45

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 - AngulardfsqView Answer on Stackoverflow
Solution 2 - Angularuser8707279View Answer on Stackoverflow
Solution 3 - AngularpabloRNView Answer on Stackoverflow
Solution 4 - AngularShashwat GuptaView Answer on Stackoverflow