user12525404
user12525404

Reputation:

Which Angular Pipe to round number AND cut out its decimals?

myNumber is a number like '2.74' and I want to display it in HTML via interpolation like this:

<div> {{ myNumber | myCurrencyEuroPipe }} </div>

But I want to round the number to the whole number AND then not display any decimals. So e.g. from '2.74' to '3'. How can I achieve this, ideally with angular pipes? I already use a custom myCurrencyEuroPipe just to display an € at the end. Could you provide an example if Angular has its own pipes for this, or is a custom one necessary?

Upvotes: 0

Views: 1154

Answers (2)

Mohamad Aboshosha
Mohamad Aboshosha

Reputation: 11

You can use toFixed()

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'myCurrencyEuro'
})
export class MyCurrencyEuroPipe implements PipeTransform {

  transform(value: number): string {
    return Number(value).toFixed() + '€';
  }

}

Upvotes: 1

Apoorva Chikara
Apoorva Chikara

Reputation: 8773

In your myCurrencyEuroPipe add this piece of code to make it work:

transform (value: number) {
      let result = value % 2 >= 0.5 ? Math.ceil(value) : Math.floor(value);
      // now pass this result to add the currency symbol.
}

Upvotes: 0

Related Questions