browser locale, comma as decimal point and toLocaleString

Discussion in 'JavaScript' started by claude_v, Feb 16, 2007.

  1. #1
    Hi!

    I have a bunch of numbers I show in the browser's locale using Number.toLocaleString(). In France for example the ',' is going to be used as a separator.

    My issue is that those fields are input/output and I need to convert the user data back to number, and Number(string) will not recognize the ',' -> NAN.

    I do not see any symmetrical operation to toLocaleString() that would let me do that (something like NumberFormat.parseLocaleString(string). Before I do my own regexp, I just want to make sure I'm not missing something.

    Thanks!

    Claude
     
    claude_v, Feb 16, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    You can try replacing ',' with '.'
    Number( string.replace(',', '.') );
    If there is '.' instead of ',' nothing is replaced.
     
    ajsa52, Feb 16, 2007 IP
  3. claude_v

    claude_v Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks! That's a good idea as long as . is not used as the thousand separator.
    Claude
     
    claude_v, Feb 16, 2007 IP