<!-- JavaScript:Maths code snippet 3. -->
<!-- Working with strings: conversion using 'parseFloat()', 'parseInt()' and 'Number()'.
Examples of use of above with strings: '42.50', '42.50text' and 'text'.
Introducing 'typeof()' which returns the type of data that has been passed to it.
Using 'isNaN()' on the data type.
Experiment with using 'minus 0' technique on a string to convert to a number. -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Code source: http://www.justfigures.co.uk/ -->
<!-- A resource for web developers using XHTML, CSS, JavaScript, PHP -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View code snippet page</title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
// create a string in the format '42.50'
var type1='42.50';
document.write("Examples of conversion of the string 'type1': " + type1 +"<br />");
document.write("typeof: " + typeof(type1) + "<br />");
document.write("Is Not a Number? isNaN: " + isNaN(type1) + "<br />");
document.write("Using 'parseFloat()' returns: " + parseFloat(type1) + "<br />");
document.write("Using 'parseInt()' returns: " + parseInt(type1) + "<br />");
type1=Number(type1);
// Note that we are not using the keyword 'new' with Number() this time
document.write("Using 'Number()' returns: " + type1 + "<br />");
document.write("typeof: " + typeof(type1) + "<br />");
document.write("Is Not a Number? isNaN: " + isNaN(type1) + "<br />");
document.write("<br />");


// create a string in the format '42.50text'
var type2='42.50text';
document.write("Examples of conversion of the string 'type2': " + type2 +"<br />");
document.write("typeof: " + typeof(type2) + "<br />");
document.write("Is Not a Number? isNaN: " + isNaN(type2) + "<br />");
document.write("Using 'parseFloat()' returns: " + parseFloat(type2) + "<br />");
document.write("Using 'parseInt()' returns: " + parseInt(type2) + "<br />");
type2=Number(type2);
// Note that we are not using the keyword 'new' with Number() this time
document.write("Using 'Number()' returns: " + type2 + "<br />");
document.write("typeof: " + typeof(type2) + "<br />");
document.write("Is Not a Number? isNaN: " + isNaN(type2) + "<br />");
document.write("<br />");

// create a string in the format 'text'
var type3='text';
document.write("Examples of conversion of the string 'type3': " + type3 +"<br />");
document.write("typeof: " + typeof(type3) + "<br />");
document.write("Is Not a Number? isNaN: " + isNaN(type3) + "<br />");
document.write("Using 'parseFloat()' returns: " + parseFloat(type3) + "<br />");
document.write("Using 'parseInt()' returns: " + parseInt(type3) + "<br />");
type3=Number(type3);
// Note that we are not using the keyword 'new' with Number() this time
document.write("Using 'Number()' returns: " + type3 + "<br />");
document.write("typeof: " + typeof(type3) + "<br />");
document.write("Is Not a Number? isNaN: " + isNaN(type3) + "<br />");
document.write("<br />");

// experiment with subtracting 0 from the string
var type4='42.50'
document.write("Conversion of the string " + type4 + " using 'minus 0':<br />");
document.write("typeof: " + typeof(type4) + "<br />");
document.write("Is Not a Number? isNaN: " + isNaN(type4) + "<br />");
type4=type4-0
document.write("Using 'minus 0' returns: " + type4 + "<br />");
document.write("typeof: " + typeof(type4) + "<br />");
document.write("Is Not a Number? isNaN: " + isNaN(type4) + "<br />");

//]]>
</script>
</body>
</html>