4

I have a number say 2,500.00 and i want to convert the number into 2.500,00. So, we can replace the special character using replace like

var x = 2,500.00; 
x.replace(/,/g,".");

and for "Dot" also, we can do it. But in this case, it won't work because when we apply replace function for comma as above, the number will become 2.500.00 and if we apply now, it will become as 2,500,00.

So is there any way to convert 2,500.00 into 2.500,00 ?

1983
  • 5,882
  • 2
  • 27
  • 39
Arun AK
  • 4,353
  • 2
  • 23
  • 46
  • 2
    Replace to something else first, like a `*`. For instance `,` -> `*`, then `.` -> `,` and finally `*` -> `.`. – evolutionxbox Jul 28 '15 at 15:16
  • Thanks for your help evolutionbox, but its a kind of workaround i think so. Is there any straight method for this? – Arun AK Jul 28 '15 at 15:18
  • There is no direct function that is cross browser compatible (not IE11+). – evolutionxbox Jul 28 '15 at 15:21
  • If you're working with numbers you can also use http://numeraljs.com to parse and reformat numbers. – Strelok Jul 28 '15 at 15:21
  • `var x = 2,500.00` is a syntax error. – 1983 Jul 28 '15 at 15:23
  • What about [this SO post](http://stackoverflow.com/questions/4951738/culture-sensitive-parsefloat-function-in-javascript)? Does it help? – Wiktor Stribiżew Jul 28 '15 at 15:23
  • thanks for the post stribizhev. But its not working in my case, but learned something in that post. – Arun AK Jul 28 '15 at 15:26
  • @KingMob seems to have a really nice solution for any number of dots and commas. The currently accepted anubhava solution works only for the specific case where there's one dot and no commas after the dot. Otherwise, it fails. – ChisholmKyle Aug 07 '15 at 10:57
  • @ChisholmKyle Actually in my case, its a number, so this solution will work for me – Arun AK Aug 07 '15 at 11:42

5 Answers5

3

String.prototype.replace can take a function:

'2,123,500.00'.replace(/[,.]/g, function(c){ return c===',' ? '.' : ','; });
1983
  • 5,882
  • 2
  • 27
  • 39
  • @Karl-AndréGagnon Oh. Where? – 1983 Jul 28 '15 at 15:40
  • Actually, I was mistaken (and learned something new). I though the `.` would match every characters... – Karl-André Gagnon Jul 28 '15 at 15:43
  • @Karl-AndréGagnon It does match every character, but not when it's inside a character class. The character class matches stuff _literally_. See here: http://www.regular-expressions.info/charclass.html#special – Pluto Jul 28 '15 at 16:20
  • @Pluto That's not entirely true, e.g. \s in a character class still matches whitespace. – 1983 Jul 28 '15 at 16:31
  • Of course it does, that's why I linked to the other document for further explanation. – Pluto Jul 28 '15 at 16:32
  • King Mob : I have a small doubt. Here, what c is actually. Why it is used in the statement? – Arun AK Jul 28 '15 at 17:08
  • 1
    @Thinker It's the matched character which is passed to the replacement function. You could rename it to `match` if that's clearer. – 1983 Jul 28 '15 at 18:39
2

You can use:

var x = '2,123,500.00'; 
var arr = x.split('.');
var y = arr[0].replace(/,/g, '.') + ',' + arr[1];
//=> 2.123.500,00
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

You're in luck, .replace() accept a function as second argument. That function has the matched string as argument and the returned value will be the replace_by value of .replace().

In short, you can simply check what the matched string is and return the right value :

var str = "2,500.00";

var changed_str =  str.replace(/,|\./g, function(old){
    if (old === '.')
        return ',';
    else if (old === ',')
        return '.';
});


document.write(changed_str)
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
0

Why not use the built-in methods to format your numbers correctly?

Number.toLocaleString() would work just fine here.

If you actually have a number as you said, you can easily achieve this using the right locale. If you have a String representation of your number, you would first have to parse it.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
-1

This (now) works for any number of commas or dots, even if trailing or leading dots or commas.

HTML:

<div id="result"></div>

JS:

var x = '.,2.123,50.0.00.';

var between_dots = x.split('.');
for (var i = 0; i < between_dots.length; i++) {
    between_dots[i] = between_dots[i].replace(/,/g, '.');
}
var y = between_dots.join(',');

document.getElementById('result').innerHTML = y;

Here's the JSFiddle

ChisholmKyle
  • 449
  • 3
  • 10