Jump to content

Very Quick, Easy Question


Prismatic

Recommended Posts

Hello,I was wondering if there was a JavaScript function that allowed the transfer of a variable's value from that of a string to a math object. I know how to use the to string method to do the opposite of this, but is the vice-versa also possible? I have searched for quite a while to find such a funtion, and I'm wondering now if it even exists.For example, if I have a variables value set to " a = '1' ," is there any way I can change the value of "a" to read, " a = 1 "?Thanks for reading. :)

Link to comment
Share on other sites

Hi,:) Yes I can, but this is not of any use What ever u want that is no of useFro that typeLike if A=1Then u want to Store in A="A=1"That is naaaaYa it is Simple but From this What u want to Do man?Tell me What u want Exactly with ur req :)

Link to comment
Share on other sites

Hi vijay, Prismatic,I think what Prismatic is looking for is javascript's "Number()" or "parseInt()".

The Number() function converts the value of an object to a number.
The parseInt() function parses a string and returns an integer.
A link reference for Number():http://www.w3schools.com/jsref/jsref_Number.aspparseInt():http://www.w3schools.com/jsref/jsref_parseInt.aspparseFloat():http://www.w3schools.com/jsref/jsref_parseFloat.aspTo my knowledge there is no function that converts the variable, you have to reassign it:
<script type="text/javascript">var a = "125.35";alert(a + 3); //a treated as Stringalert(Number(a) + 3);alert(a + 3); //a is still a Stringa = Number(a);alert(a + 3);</script>

Thanks,

Link to comment
Share on other sites

Also, if you're using math functions with variables, you can do this:

x="1"y="1"z=eval("x+y")alert(z)

That would alert the number 2. The eval function does that. :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...