Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting number to India Locale Format

I was trying to convert a number say 123456.789 to India Locale Format wiz 1,23,456.789 as India uses thousands/lakh/crore separators.

I was successfully able to do this with some 10-20 lines of code and then I came to this article of MDN.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#Using_locales

You see the line 11, they have declared a mechanism there to convert it

// India uses thousands/lakh/crore separators
console.log(number.toLocaleString('en-IN'));
// → 1,23,456.789

But it is not working (tested in Chrome v49.0.2623 and Safari)

Output in safari:
123456.789

Output in chrome:
123,456.789

But it worked in firefox

Output in Firefox:
1,23,456.789

But when I read the browser compatibility table it was stated that it is supported in chrome v24+

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#Browser_compatibility

Is it a bug or am I doing something wrong?

var number = 123456.789
console.log(number.toLocaleString('en-IN'));
like image 232
void Avatar asked Apr 04 '16 10:04

void


1 Answers

This simply means that the en-IN locale code isn't supported by Chrome (or Safari). You may have better luck using a different locale which uses the same format. An example of this is the Hindi locale code (hi), which seems to return this format in Chrome, Firefox and Internet Explorer 11:

console.log((123456.789).toLocaleString('hi'));
-> 1,23,456.789

For what it's worth, MDN's support table only lists Basic Support. It determines which browsers implement the functionality, but it doesn't go as far as testing every possible use case.

like image 190
James Donnelly Avatar answered Oct 04 '22 14:10

James Donnelly