Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular pipe formatting for millions

Tags:

angular

pipe

I'm working with big numbers that I'm now formatting like this

{{total  | number:'1.0-0'}}

and the outcome is 45,986,592. I need to format the number to just have the millions and then just one digit after, so 45.9

How can I achieve this?

like image 273
muffinlou Avatar asked Oct 20 '25 14:10

muffinlou


1 Answers

You can built a custom pipe if you want to reuse this logic.

As Phil suggested, you can just divide it by 1,000,000

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

@Pipe({
  name: 'million'
})
export class MillionPipe implements PipeTransform {

  constructor(private decimalPipe: DecimalPipe) {

  }
  transform(value: any, digits?: any): any {
    return this.decimalPipe.transform(value/1000000, digits)
  }

}

Make sure you have the decimal pipe registered in your module provider so it can be injected.

Working Demo:

https://stackblitz.com/edit/angular-aa16so?file=app%2Fmillion.pipe.ts

like image 151
Ringo Avatar answered Oct 23 '25 04:10

Ringo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!