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?
add package to Node Modules folder with
yarn add numeral
or
npm install numeral
then import into Typescript file
import * as numeral from 'numeral';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With