Last active
June 14, 2016 01:12
-
-
Save yahyaKacem/541c0ee70d01586d751f235ef52ad557 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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