Skip to content

Instantly share code, notes, and snippets.

@yahyaKacem
Last active June 14, 2016 01:12
Show Gist options
  • Select an option

  • Save yahyaKacem/541c0ee70d01586d751f235ef52ad557 to your computer and use it in GitHub Desktop.

Select an option

Save yahyaKacem/541c0ee70d01586d751f235ef52ad557 to your computer and use it in GitHub Desktop.

Revisions

  1. yahyaKacem revised this gist Jun 14, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions humanize.pipe.ts
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // based on https://gist.github.com/maggiben/9457434
    import { Pipe, PipeTransform } from '@angular/core';

    @Pipe({name: 'humanize'})
  2. yahyaKacem created this gist Jun 14, 2016.
    16 changes: 16 additions & 0 deletions humanize.pipe.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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];
    }
    }