Java Reference
In-Depth Information
Next, you write to the page the converted integer value of myString displayed inside a user‐friendly
sentence you build up using string concatenation. Notice that you use the escape sequence \" to display
quotes ( " ) around the string you are converting:
document.write("\"" + myString + "\" is " + parseInt(myString, 10) +
" as an integer" + "<br/>");
As you can see, you can use parseInt() and parseFloat() in the same places you would use a
number itself or a variable containing a number. In fact, in this line the JavaScript engine is doing
two conversions. First, it converts myString to an integer, because that's what you asked for by
using parseInt() . Then it automatically converts that integer number back to a string, so it can be
concatenated with the other strings to make up your sentence. Also note that only the 56 part of the
myString variable's value is considered a valid number when you're dealing with integers. Anything
after the 6 is considered invalid and is ignored.
Notice the second value, the number 10 , that is passed to parseInt() . This is called the radix, and it
determines how the string is parsed into a number. By passing the number 10 , you tell the parseInt()
function to convert the number using the Base 10 number system. Base 10 is our common number
system, but you can use parseInt() to convert numbers to binary (Base 2), hex (Base 16), and other
number systems. For example, parseInt(10, 2) converts the number 10 using the binary number
system, resulting in the number 2 . Always specify the radix! Without it, JavaScript guesses what
number system to use, and you could encounter unexpected results.
Next, you do the same conversion of myString using parseInt() , but this time you store the
result in the myInt variable. On the following line you use the result in some text you display to the
user:
myInt = parseInt(myString, 10);
document.write("\"" + myString +
"\" when converted to an integer equals " + myInt + "<br/>");
Again, though myInt holds a number, the JavaScript interpreter knows that +, when a string and a
number are involved, means you want the myInt value converted to a string and concatenated to the
rest of the string so it can be displayed.
Finally, you use parseFloat() to convert the string in myString to a floating‐point number, which
you store in the variable myFloat . This time the decimal point is considered to be a valid part of the
number, so it's anything after the 2 that is ignored. Again you use document.write() to write the result
to the web page inside a user‐friendly string:
myFloat = parseFloat(myString);
document.write("\"" + myString +
"\" when converted to a floating point number equals " + myFloat);
dealing with strings that Won't Convert
Some strings simply are not convertible to numbers, such as strings that don't contain any numerical
data. What happens if you try to convert these strings? As a little experiment, try changing the
 
Search WWH ::




Custom Search