i want to remove comma from a number [e.g change 1,125 to 1125 ] in a .tpl file. The value comes dynamically like ${variableMap[key]}
John Conde
215k98 gold badges447 silver badges489 bronze badges
asked Sep 24, 2012 at 5:18
0
var a='1,125';
a=a.replace[/\,/g,'']; // 1125, but a string, so convert it to number
a=parseInt[a,10];
Hope it helps.
answered Sep 24, 2012 at 5:23
web-nomadweb-nomad
5,9253 gold badges33 silver badges49 bronze badges
6
var a='1,125'
a=a.replace[/\,/g,'']
a=Number[a]
Nissa
4,6088 gold badges29 silver badges37 bronze badges
answered Mar 6, 2017 at 21:53
brianyangbrianyang
1,04012 silver badges13 bronze badges
1
You can use the below function. This function can also handle larger numbers like 123,123,123.
function removeCommas[str] {
while [str.search[","] >= 0] {
str = [str + ""].replace[',', ''];
}
return str;
};
answered Jan 28, 2015 at 18:59
Aiden ZhaoAiden Zhao
5345 silver badges24 bronze badges
var s = '1,125';
s = s.split[','].join[''];
Hope that helps.
answered May 24, 2016 at 22:09
Cezary TomczykCezary Tomczyk
4961 gold badge7 silver badges13 bronze badges
You can use Regular Expression to change as it is faster than split join
var s = '1,125';
s = s.replace[/,/g, ''];
//output 1125
answered Aug 5, 2020 at 12:03
levislevis
4125 silver badges14 bronze badges
Incoming value may not always be a string. If the incoming value is a numeric the replace method won't be available and you'll get an error. Suggest using isNaN to see if numeric, then assume string and do replacement otherwise.
if[isNaN[x]] {
x = parseInt[x.replace[/[,]/g,'']];
}
[Not foolproof because 'not number' doesn't prove it is a string, but unless you're doing something very weird should be good enough]. You can also add other symbols to the character group to remove other stray chars [such as currency symbols].
answered Nov 6, 2020 at 11:04
1
✨ ES2021 ✨ added replaceAll
, so no need for regular expression:
const str = '1,125.05';
const number = parseFloat[str.replaceAll[",", ""]];
answered Sep 14 at 8:15
Amir2miAmir2mi
3266 silver badges9 bronze badges