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

01-11-2004, 12: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)


01-11-2004, 01: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, 03:44 AM
Also the Number (http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/toplev.html#1064092) function is another choice.

01-11-2004, 11:00 PM
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:


Instead, it returned:


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"
alert (newnum)

<script type="text/javascript" language="javascript">
var num1=250
var num2="100"

01-11-2004, 11:23 PM
Ah, the input is not changed when you use those functions - the output from them is changed, however. An example:
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

01-12-2004, 12:26 AM
thanks, that did it. I appreciate the help. I'm glad I found this place.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum