JavaScript character conversion numeric function

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax create document function functions javascript note return

In the JS to convert the character to the number of parseint () parsefloat () () Three functions, let us explain their syntax and use methods.

About the number function

The number object is the wrapper object for the original numeric value.

To create the syntax for a number object:
var mynum=new number (value);
var mynum=number (value);
The parameter value is the number of the number object that you want to create, or the value you want to convert to numbers.

return value
When number () and operator new are used together as constructors, it returns a newly created number object. If you do not use the new operator to call number () as a function, it converts its arguments to an original numeric value and returns this (Nan) if the conversion fails.

Instance

var big = Number.MAX_VALUE
Alert (number (' 9999999.9999999 ')
Result 9999999.9999999
Alert (number (' 99999999.99999999 ')
Result 99999999.99999998
Alert (number (' 99999999.999999999 ')
Result 100000000


parseint Introduction

The parseint () function parses a string and returns an integer.

Syntax
parseint (string, radix)

When the value of the parameter radix is 0, or if the parameter is not set, parseint () determines the cardinality of the number based on a string.

Note: Only the first digit in the string is returned.

Note: The opening and trailing spaces are allowed.

Tip: If the first character of a string cannot be converted to a number, then parsefloat () returns Nan.
Instance

parseint ("10"); Return 10
parseint ("19", 10); return 19 (10+9)
parseint ("11", 2); Return 3 (10+9)
parseint ("17", 8); Return 15 (8+7)
parseint ("1f", 16); Return 31 (16+15)
parseint ("010"); Undecided: return 10 or 8

Example Two

Parsefloat ("1234blue"); Returns 1234.0
Parsefloat ("0xa"); Returns Nan
Parsefloat ("22.5"); Returns 22.5
Parsefloat ("22.34.5"); Returns 22.34
Parsefloat ("0908"); Returns 908
Parsefloat ("Blue"); Returns Nan


parsefloat function

The parsefloat () function parses a string and returns a floating-point number.

Syntax
Parsefloat (String) parameter description
X required. The string to be parsed.

return value
Returns the parsed number.
Tips and comments
Note: Only the first digit in the string is returned.

Note: The opening and trailing spaces are allowed.

Tip: If the first character of a string cannot be converted to a number, then parsefloat () returns Nan.

Tip: If you want to resolve only the integer portion of a number, use the parseint () method.


Instance

document.write (parsefloat ("10"))
document.write (parsefloat ("10.00"))
document.write (parsefloat ("10.33"))
10
10
10.33


<html>
<head>
<!--
<script type= "text/web Effects" src= "Parseint.js" ></script

<script type= "Text/javascript"
<!--
 function result (str)
 {
  document.write (str) (parseint);
 }

</script>
<title></title>
</head>
<body>
parseint ("0") =<script type= "Text/javascript" >result ("0") </script><br></br>
parseint ("a") =< Script type= "Text/javascript" >result ("a") </script><br></br>
parseint ("1") =<script Type= "Text/javascript" >result ("1") </script><br></br>
parseint ("") =<script type= " Text/javascript ">result (") </script><br></br>
parseint ("7") =<script type= "text/ JavaScript ">result (" 7 ") </script><br></br>
parseint (" modified ") =<script type=" text/ JavaScript ">result" ("Modified") </SCRIPT><BR></BR>
parseint ("8") =<script type= "Text/javascript" >result ("8") </script><br></br>
parseint ("") =<script type= "Text/javascript" >result ("") </script><br></br>
parseint ("9") =<script type= "Text/javascript" >result ("9") </script><br></br>
parseint ( "a") =<script type= "Text/javascript" >result ("a") </script><br></br>
parseint ("10") = <script type= "Text/javascript" >result ("ten") </script><br></br>
</body>
</ Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.