Search code examples
angularangular-pipe

Show two decimal places if decimal part is greater than 0 in angular 7


I am working on an Angular 7 application. How to format the numbers as below:

2.14569 to 2.14
3.245 to 3.24
1.00 to 1

How can i achieve this in Angular. I have tried (| number:'1.2-2') this but it always give two decimal places like 4.00 but i want it to be 4. Basically my problem is that, if there is decimal part and that is greater than 0 then show only two decimal places otherwise just show simple number without decimal part.


Solution

  •     // Data 
    decimalValue = {
            one: 2.14569,
            two: 3.245,
            three: 1.00
          }
    
    // HTML
        <div>{{decimalValue.one | number:'1.0-2'}}</div>
        <div>{{decimalValue.two | number:'1.0-2'}}</div>
        <div>{{decimalValue.three | number:'1.0-2'}}</div>