View Full Version : Converting "text" number to an actual number

xbuddy

01-11-2004, 01:56 AM

In recent postings I was shown how to pass numbers from one web page to another. That works great. The problem I have now is the numbers that were passed are text instead of numbers. Listed below are the numbers that were passed:

num1 = 100

num2 = 200

num3 = 300

If I were to add these numbers together on Page2.htm, the result would look like 100200300 instead of 600. Any ideas as I would convert these to actual numbers?

The posting where I obtain the coding:

click here (http://www.codingforums.com/showthread.php?s=&postid=162241#post162241)

http://www.codingforums.com/showthread.php?s=&postid=162241#post162241

liorean

01-11-2004, 02:58 AM

parseInt( string, 10 ); // Integers with a radix of 10. (IOW, decimal integers)

parseFloat( string ); // Floating point numbers, radix of 10. (IOW, decimal floating point numbers)

Jeff Mott

01-11-2004, 04:44 AM

Also the Number (http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/toplev.html#1064092) function is another choice.

xbuddy

01-12-2004, 12:00 AM

Thanks guys for your help. I'm still having problems, however, converting the textstring numbers to numeric numbers. To test the conversion, I created two numbers, one as a text string and the other as a numeric number. I used the number function, but it didn't convert it. It should have returned the answer:

350.

Instead, it returned:

250100.

Any ideas? I also used the parseInt and got the same results. Listed below are the codes I used.

<script type="text/javascript" language="javascript">

var num1=250

var num2="100"

Number(num2)

newnum=num1+num2

alert (newnum)

</script>

<script type="text/javascript" language="javascript">

var num1=250

var num2="100"

Number(num2)

parseInt(num2,10);

newnum=num1+num2

alert(newnum)

</script>

liorean

01-12-2004, 12:23 AM

Ah, the input is not changed when you use those functions - the output from them is changed, however. An example:

var

sNumber = '100.1', // => [string '100.1']

iNumber = parseInt( sNumber ), // => [number 100]

nNumber = parseFloat( sNumber ); // =>[number 100.1]

/* The results?

sNumber is still a string '100.1'

iNumber is a number 100

and nNumber is a number 100.1

*/

xbuddy

01-12-2004, 01:26 AM

thanks, that did it. I appreciate the help. I'm glad I found this place.

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.