Hi,
I'm new to anuglar.js. I like to know, how to specify a number without group separation format.
For e.g:
12345678.01234
If I specify particular {{ var1 | number }} , comes with comma(,) separation for en and dot(.) separation for es locale.
I came to know that, the existing API doesn't support the custom number formatting. I like to know, how to overwrite the existing API to support the custom number formatting.
Please help me out!!
hi Shyamsundar,
Your #1 is plain unworkable. have a look at the i18n stuff from angular. I think for most of what you need you just need to load another locale.
Your #2 will work. And while your sample will take (on first sight) in account most, if not all, edge-cases, it is a lot of code. It might be that you need all of this, but for just some number formatting, it is a lot.
Most users will never hit the exponents. And while it’s very flexible, and you can choose whatever formatting you like, it might be a better idea to just code for the case you have, in stead of creating a solution for every possible use-case.
meaning, if you have 2 numbers to display on the page, this is not an issue. If you have to display a few hundreds of numbers, all those extra processing cycles will add up.
a simple formatter like this:
function myFormat() {
return myFormatter;
function myFormatter(num) {
if (!angular.isNumber(num)) {
// don't process anything that's not a number
return num;
}
var string=''+Math.round(+num * 100);
var len = string.length;
var l = len;
var result = [];
while (--l>-1) {
result.unshift('' + string[l]);
if (len-l===2) {
//decimail point
result.unshift(',');
} else if (l > 0 && (len-l-2)%3 === 0 ) {
// separator
result.unshift('.');
}
}
return result.join('');
}
}
Will do in most cases, (see it in action)
Does this help a bit?
Regards
Sander