Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript toFixed localized?

Do you know if toFixed is a localized function?

I mean, will this:

var n = 100.67287; alert(n.toFixed(2)); 

show "100.67" on english US OS/browsers and "100,67" (with comma) on Italian OS/browsers? (Italian or any other local system that uses comma as decimal separator).

Thanks!

like image 634
Marco Demaio Avatar asked May 19 '10 13:05

Marco Demaio


2 Answers

Late addition: with Number.toLocaleString() now available on everything bar IE 10 & below, this works, albeit rather long-winded:

var n = 100.67287; console.log(n.toLocaleString(undefined, {   minimumFractionDigits: 2,   maximumFractionDigits: 2 }));

Using undefined or 'default' for the language code will use the browser default language to format the number.

See developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString for full details.

If you're free to extend the Number prototype, you could defined Number.toLocaleFixed().

like image 154
ChrisV Avatar answered Sep 21 '22 06:09

ChrisV


No, this will always return a point. The ECMA 262-spec [15.7.4.5] states it should be a point.

like image 44
Robert Massa Avatar answered Sep 22 '22 06:09

Robert Massa