11

i've this simple function:

Chrome, Firefox, IE:

Number(1000000).toLocaleString()
"1 000 000" // in french system, the space is the separator instead of the comma

Opera, Maxthon:

Number(1000000).toLocaleString()
"1000000"

why Opera and Maxthon cant format it? they support this method but dont execute it in the right way?

is there any toLocaleString() replacement?

Abdelouahab Pp
  • 4,252
  • 11
  • 42
  • 65
  • 1
    Just pointing out that you should also not expect uniform output across your users `Number(1000000).toLocaleString();` gives me `"1,000,000"` (Google Chrome, en-GB). See the [**MDN page**](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toLocaleString) for what parameters it should take and in which browsers it is supported. – Paul S. Apr 22 '13 at 22:27
  • @PaulS. am sorry, forgot to precise that i use french OS, and in french system, the separation is done using a space instead of the comma – Abdelouahab Pp Apr 22 '13 at 22:30
  • it seems that it was also absent in Chrome! sorry because i thought that a method MUST return always a result if it is there! – Abdelouahab Pp Apr 22 '13 at 22:49

2 Answers2

16

The output will also be different depending on the user's locale settings, even if Number.prototype.toLocaleString is supported by their browser, e.g. for me on en-GB, Number(1000000).toLocaleString(); gives me "1,000,000".

is there any toLocaleString() replacement?

Why not write one to do exactly what you want? For example,

function localeString(x, sep, grp) {
    var sx = (''+x).split('.'), s = '', i, j;
    sep || (sep = ' '); // default seperator
    grp || grp === 0 || (grp = 3); // default grouping
    i = sx[0].length;
    while (i > grp) {
        j = i - grp;
        s = sep + sx[0].slice(j, i) + s;
        i = j;
    }
    s = sx[0].slice(0, i) + s;
    sx[0] = s;
    return sx.join('.');
}

Now

localeString(1000000.00001);
// "1 000 000.00001"
Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • ah! that will do the job! thank you – Abdelouahab Pp Apr 22 '13 at 22:43
  • because am coming from python, i thought it was simple, the metho was to invert the stringm forat it, then re-invert it.... but it will make lot of lines! in python if you want to invert a string, you just make a negative step, `"hello"[::-1]` and the latest element is the negative one, for example the `o` will be `"hello"[-1]` really the world is hard from someone coming from python ;) thnk you again – Abdelouahab Pp Apr 22 '13 at 22:47
  • 2
    `'abc'.split('').reverse().join('')` is `'cba'`, but you still would need to loop up over it like this. Just loop down instead. Further, `'abc'.slice(-1)` is `'c'`. – Paul S. Apr 22 '13 at 22:50
  • ah! i should take more courses about javascript, thank you again :) – Abdelouahab Pp Apr 22 '13 at 22:53
  • I legit can't understand this code, one reason why I may not want to write a solution to do this myself. –  Mar 29 '17 at 02:52
  • @AlexanderPritchard I make use of a couple tricks: line 2, `''+x` converts `x` to a _String_, which can then be `.split` on the decimal point, giving you access to a string version of the pre and post decimal point bits of the number as Strings in an Array (length 2). Lines `3` and `4` are just ways to set defaut values to the inputs. Next, the loop lets you do stuff on sections of the string in the size of the number grouping you want. This loop looks a bit weird because it's going from right-to-left, i.e. so we get `1 000` and not `100 0` – Paul S. Apr 01 '17 at 06:58
4

The language spec leaves the definition very open-ended:

15.7.4.3 Number.prototype.toLocaleString()

Produces a String value that represents this Number value formatted according to the conventions of the host environment’s current locale. This function is implementation-dependent, and it is permissible, but not encouraged, for it to return the same thing as toString.

Different browsers are allowed to implement it differently, and can implement it differently based on the locale chosen by the user.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245