I'm trying to remove the comma from a number.
var thisbill_str = "";
thisbill = $('#linebill_' + z).val();
if (isNaN(thisbill) ) { thisbill = 0.00; }
thisbill_str = thisbill;
thisbill = thisbill_str.replace(/,/g, "");
This javascript code is giving me an error:
TypeError: thisbill_str.replace is not a function
It doesn't matter if I give it the 'g' and 'i' flags. It only does it if the string does NOT already have a comma in it, i.e., if the string is '515.00'. If the string were '5,515.00', then it works fine. I don't see anything in the documentation that tells me that the string to be replaced has to actually exist. What am I missing, and what is a better way to do this?
I'm trying to remove the comma from a number.
var thisbill_str = "";
thisbill = $('#linebill_' + z).val();
if (isNaN(thisbill) ) { thisbill = 0.00; }
thisbill_str = thisbill;
thisbill = thisbill_str.replace(/,/g, "");
This javascript code is giving me an error:
TypeError: thisbill_str.replace is not a function
It doesn't matter if I give it the 'g' and 'i' flags. It only does it if the string does NOT already have a comma in it, i.e., if the string is '515.00'. If the string were '5,515.00', then it works fine. I don't see anything in the documentation that tells me that the string to be replaced has to actually exist. What am I missing, and what is a better way to do this?
Share Improve this question edited Jun 7, 2014 at 10:19 user2864740 61.9k15 gold badges157 silver badges227 bronze badges asked Sep 18, 2013 at 1:19 McAuleyMcAuley 4241 gold badge3 silver badges14 bronze badges 1 |3 Answers
Reset to default 12It is most likely caused by thisbill_str being something other than a String. Maybe you have some other code somewhere that automatically converts thisbill_str to a Number?
You can convert back to a string using String(thisbill_str).
The complete code would be:
thisbill = String(thisbill_str).replace(",", "")
thisbill_str = thisbill;
You should be casting to a string here. thisbill_str
is still a number, so it doesn't have a replace method.
thisbill_str = thisbill + '';
TypeError: thisbill_str.replace is not a function
thisbill_str
can't be coerced in to a String object, therefore it doesn't have the replace
method.
Is the value of thisbill_str
null?
thisbill_str
has in it when it doesn't work. Even better to make a jsFiddle that shows the problem. Probablythisbill_str
isn't a string. – jfriend00 Commented Sep 18, 2013 at 1:21