Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Numeral.js library in angular2/ionic 2 typescript app?

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?

like image 885
Sukumar MS Avatar asked Jun 12 '17 07:06

Sukumar MS


1 Answers

add package to Node Modules folder with

yarn add numeral

or

npm install numeral

then import into Typescript file

import * as numeral from 'numeral';

like image 58
Zagazag Avatar answered Sep 29 '22 08:09

Zagazag