Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting numeric values from a string variable

Status
Not open for further replies.

asfaw

Programmer
Jun 28, 2004
36
CA
How can I remove a comma in string e.g. 1,256.50

I would like to remove the ',' and store 1256.50 in a new variable. What I am trying to do is get the numeric value.

Regards,

asfaw
 
Code:
<script type="text/javascript">
function blah(str) {
   return parseFloat(/[,]/g.replace(""));
}
</script>
<input type="text" id="txt">
<input type="button" value="show number" onclick="alert(blah(document.getElementById('txt').value))">

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Code:
var s = "1,256.50";

//  strip commas
s = s.replace(/,/g, "");

//  get numeric value
var n = parseFloat( s );

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
I am not sure I understand your response. You have not used str in the return statement. I am not sure why.

function blah(str) {
return parseFloat(/[,]/g.replace(""));
}

Regards,

asfaw
 
nah, not what kaht said......

I horribly butchered that function.....


I didn't even make reference to my variable str in it. That's what I get from trying to answer questions from home on a Saturday night. My brain's just not in it.

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
It was a mistake on my behalf, I just got home from the movies and I shouldn't be answering questions [lol]

replace is a String method that accepts 2 arguments, a regular expression and a string for replacement. Jeff used the correct syntax above, use his solution.

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Thank you all for your kind help. You have helped me a lot.

Best regards,

asfaw
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top