Skip to content

Instantly share code, notes, and snippets.

@yahyaKacem
Last active June 14, 2016 01:12
Show Gist options
  • Save yahyaKacem/541c0ee70d01586d751f235ef52ad557 to your computer and use it in GitHub Desktop.
Save yahyaKacem/541c0ee70d01586d751f235ef52ad557 to your computer and use it in GitHub Desktop.
// based on https://gist.github.com/maggiben/9457434
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'humanize'})
export class Humanize implements PipeTransform {
transform(value: number): any {
let si: string[], exp: number, result: any;
if (value < 1000) {
return value;
}
si = ['K', 'M', 'G', 'T', 'P', 'H'];
exp = Math.floor(Math.log(value) / Math.log(1000));
result = value / Math.pow(1000, exp);
result = (result % 1 > (1 / Math.pow(1000, exp - 1))) ? result.toFixed(2) : result.toFixed(0);
return result + si[exp - 1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment