/    Sign up×
Community /Pin to ProfileBookmark

How to convert a string/char to an int?

Hi,

I have auser entering in an integer value via a web form, I need to convert that value to an int so that I can increment it and store it..

Is there a method in Javascript for converting a char/string to an int?

Thanks,
Asha

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@lilluSep 29.2003 — Try the parseInt function.

variable = parseInt(variable);

However, javascript is a friendly loosely-typed language and if you use math operations on strings it will assume that you actually mean numbers.... but I'd rather be on the safe side and explicitly tell javascript which data type I'm currenty using...
Copy linkTweet thisAlerts:
@XinSep 29.2003 — you might want to use parseFloat() instead, parseInt("08") will give you something wrong instead of 8.
Copy linkTweet thisAlerts:
@steelersfan88Apr 01.2004 — Using that method, try:[code=php]variable = parseInt(parseFloat("010.6"))[/code]or to avoid the parseInt alltogether, try:[code=php]
<script type="text/javascript">

var num = "010.6"
num = (parseFloat(parseFloat(num).toString().substr(0,(parseFloat(num).toString().indexOf('.') > -1) ? (parseFloat(num).toString().indexOf('.')) : (parseFloat(num).toString().length))))
alert(num)

</script>[/code]
It takes the string, makes in a number, then makes it a string, finds any period, cuts off the decimal, then returns back as a number. The first method simply makes the string a floating point decimal before converting to an integer ?
Copy linkTweet thisAlerts:
@JuuitchanApr 01.2004 — Oh no. Try this on .00000007 to see one thing wrong with it.

I've read that explicitly specifying base 10 will make parseInt work correctly.
Copy linkTweet thisAlerts:
@JuuitchanApr 02.2004 — Lillu:

Only sometimes are strings treated like numbers in math operations.

Examples:

"5"+"2" is "52"

"5"-"2" is 3 (or is it "3"? I'm not sure)
Copy linkTweet thisAlerts:
@steelersfan88Apr 02.2004 — Giving that number just displays it in scientific notation. You could do a check to see if its less than one 1, then do it, such as:[code=php]<script type="text/javascript">

var num = "-.000007"
num = (num < 1 && num > -1) ? 0 : (parseFloat(parseFloat(num).toString().substr(0,(parseFloat(num).toString().indexOf('.') > -1) ? (parseFloat(num).toString().indexOf('.')) : (parseFloat(num).toString().length))))
alert(num)

</script>[/code]
.

Strings are treated as numbers only when not using the plus operator, since it is also the concatenation operator. Other languages intelligently use the ampersand as the concatenation character to avoid this problem. "5"-"2" = 3; tested below:[code=php]<script type="text/javascript">

alert(("5"-"2")==="3") // false
alert(("5"-"2")===3) // true

</script>[/code]
To do the base 10 thing, place as: parseInt(num,10). That should do the trick ?
×

Success!

Help @ashas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...