HTML and CSS Reference
In-Depth Information
FORMAT
parseFloat(String);
EXAMPLE
parseFloat("45.3 degrees");
Table 5.18 parseFloat(String)
String
Result
"hello"
NaN
"Route 66.6"
NaN
"6.5 dogs"
6.5
"6"
6
"6.56"
6.56
EXAMPLE 5.16
<html>
<head>
<title>Using the parseFloat() Function</title>
<script type = "text/javascript">
1
var temp = prompt("What is your temperature? ", "");
2
temp=parseFloat(temp);
3
if(temp == 98.6){
4
alert("Your temp is normal");
}
5
else{
alert("You are sick!");
}
</script>
</head>
<body></body>
</html>
EXPLANATION
1
The user is prompted for input and the result is assigned as a string to the variable
temp (see Figure 5.25).
2
The parseFloat() function converts the string into a floating-point number and as-
signs it to temp .
Continues
Search WWH ::




Custom Search