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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace a hypen in a string

Status
Not open for further replies.

tester321

Programmer
Mar 13, 2007
150
CA
I've tried to use this to no avail, any suggestions:
a1=-31;
a1 = a1.replace(/-/,"");

// to get just 31
 
You have to know more rigorously data type. replace function is operating on string data type. Your a1 is somewhat an integer. In this case, why not multiply by (-1) to make it positive?! If you are at a school, you've to ask your tutor.
[tt]
//a1=a1*(-1);
//or if you want a contrived twist
a1=parseInt(a1.toString().replace(/-/,""),10);
[/tt]
 
Why not
Code:
a1 = Math.abs(a1);
if you just want the absolute value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top