Java Reference
In-Depth Information
<body>
<script>
var myString = "56.02 degrees centigrade";
var myInt;
var myFloat;
document.write("\"" + myString + "\" is " + parseInt(myString, 10) +
" as an integer" + "<br/>");
myInt = parseInt(myString, 10);
document.write("\"" + myString +
"\" when converted to an integer equals " + myInt + "<br/>");
myFloat = parseFloat(myString);
document.write("\"" + myString +
"\" when converted to a floating point number equals " + myFloat);
</script>
</body>
</html>
Load it into your browser, and you'll see three lines written in the web page, as shown in Figure 2-8.
figure 2-8  
Your first task in the script block is to declare some variables. The variable myString is declared and
initialized to the string you want to convert. You could just as easily have used the string directly in this
example rather than storing it in a variable, but in practice you'll find that you use variables more often
than literal values. You also declare the variables myInt and myFloat , which will hold the converted
numbers:
var myString = "56.02 degrees centigrade";
var myInt;
var myFloat;
Search WWH ::




Custom Search