Search code examples
angularrounding

How round calculated number in Angular 2?


I have a *ngFor loop and want to calculate a value - with 2 decimal places.

The calculation works:

 {{ ((date | amDifference : item.startdate : 'minutes' :true)/item.duration*100)  }}

But how can I round? I tryed like this:

 {{ num | ((date | amDifference : item.startdate : 'minutes' :true)/item.duration*100) : '1.2-2' }}

But this create error:

zone.js:355 Unhandled Promise rejection: Template parse errors: Parser Error: Unexpected token (, expected identifier or keyword at column 8 in [ {{ num | ((date | amDifference : item.startdate : 'minutes' :true)/item.duration*100) : '1.2-2' }}


Solution

  • You need another pipe to do this

    import {Pipe} from 'angular2/core';
    
    @Pipe({name: 'round'})
    export class RoundPipe {
      transform (input:number) {
        return Math.floor(input);
      }
    }
    

    template:

    {{ date | amDifference : item.startdate : 'minutes' : true | round }}