728x90
SMALL
#javascript
#JavaScript / Functions / parseFloat(), parseInt() - functions to replace strings with numbers
parseFloat()
parseFloat() is a function that incorrectly replaces strings.
Grammar
parseFloat( string )
If you start with a number, it incorrectly replaces that number.
If there are multiple numbers in the relief, replace only the first number.
Spaces are ignored if they start with a space.
Returns NaN if it starts with a non-digit character.
Example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript</title>
<style>
body {
font-family: Consolas, monospace;
}
</style>
</head>
<body>
<script>
document.write( "<p>parseFloat( '12.34' ) : " + parseFloat( '12.34' ) + "</p>" );
document.write( "<p>parseFloat( ' 12.34' ) : " + parseFloat( ' 12.34' ) + "</p>" );
document.write( "<p>parseFloat( '12.34 56.78' ) : " + parseFloat( '12.34 56.78' ) + "</p>" );
document.write( "<p>parseFloat( 'A 12.34' ) : " + parseFloat( 'A 12.34' ) + "</p>" );
</script>
</body>
</html>
parseInt()
parseInt() - A function that replaces a string with an integer.
Grammar
parseInt( string, n )
- Replaces a string with its value in base n. You can optionally enter n from 2 to 36. If not entered, it will be treated as 10.
- Processing strings is almost the same as parseFloat().
- The decimal part is discarded.
- If it starts with 0x, it will be processed in hexadecimal.
Example
- parseInt( '100', 2 ) : 100 is 4 in binary.
- parseInt( '0x100' ) : 100 is 256 in hex.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript</title>
<style>
body {
font-family: Consolas, monospace;
}
</style>
</head>
<body>
<script>
document.write( "<p>parseInt( '12.68' ) : " + parseInt( '12.68' ) + "</p>" );
document.write( "<p>parseInt( '100', 10 ) : " + parseInt( '100', 10 ) + "</p>" );
document.write( "<p>parseInt( '100', 2 ) : " + parseInt( '100', 2 ) + "</p>" );
document.write( "<p>parseInt( '0x100' ) : " + parseInt( '0x100' ) + "</p>" );
</script>
</body>
</html>
728x90
LIST