Angular 2 Currency Pipe

1.9k views Asked by At

I am using currency pipe in angular2, current I am using

{{price | currency:'USD':true:'1.2-2'}}

which outputs $480,000.00 But The result I want is $480k, is this possible to achieve?

2

There are 2 answers

0
rishal On BEST ANSWER

I end up creating my own custom pipe

@Pipe({
name: 'salePrice'
})

export class PricePipe implements PipeTransform {
    transform(input: number): any {


    let price: number = input;

        if (price > 3000000) {
            return '3m+';
        }

        if (price / 1000 > 1) {
            if (price / 1000000 >= 1) {
                return parseFloat((price / 1000000).toFixed(3)) + "m";
            } else {
                return parseFloat((price / 1000).toFixed(2)) + "k";
            }
        } else {
            return price;
        }
    }
}
0
Yakov Fain On

You need to write a custom pipe to replace thousands with K. You can still use the currency pipe, but chain it with your custom pipe. You can see the doc here: https://angular.io/docs/ts/latest/guide/pipes.html