5

I have been successful using Numeral.js library in my angularJs(1.x) applications to format my numbers in different formats. This is how I use angular1 filters:

filter.js

.filter('numeral', function () {
  return function (count) {
    var numericalFormat = numeral(count).format('0.0a');
    return numericalFormat;
  };
})

For that I followed the official docs of Numeral.js and installed using npm. I also give reference to nodemodules in index.html.

In the Browser

<script src="node_modules/numeral/numeral.min.js"></script>

but it shows

ERROR ReferenceError: numeral is not defined at NumeralFilter.transform

Initially I tried with using CDN reference, it works as I expected in the browser. But when I installed the app on real device I am getting an error "numeral is not defined".

pipe

import {Pipe,PipeTransform,Injectable} from "@angular/core"
@Pipe({
name:'numeralPipe'
})
@Injectable()
export class NumeralPipe implements PipeTransform{
    transform(count:any):any{ //eg: in count has value something like 52456.0
       var numericalFormat = numeral(count).format('0.0a');//error here
    return numericalFormat; // i have to format it like 52,5k
  }; 
} 

Is there any type script library for formatting and manipulating numbers, or is there any way to do this in angular2?

Mel
  • 5,837
  • 10
  • 37
  • 42
Sukumar MS
  • 748
  • 1
  • 11
  • 42

2 Answers2

9

add package to Node Modules folder with

yarn add numeral

or

npm install numeral

then import into Typescript file

import * as numeral from 'numeral';

Zagazag
  • 91
  • 2
2

add

declare var numeral:any

after

import {Pipe,PipeTransform,Injectable} from "@angular/core".
Parth Ghiya
  • 6,929
  • 2
  • 30
  • 37