Angular 2 – how round calculated number?

Issue

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

Answered By – Dimanoid

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.