The difference between two methods of converting a string into a numeric value in JS

Source: Internet
Author: User

There are three ways to convert a string into a numeric value in JS: conversion function, coercion type conversion, implicit conversion

1. Conversion functions

parseint ()//Convert a string to an integral type

Parsefloat ()//Convert a string to a floating-point type

A conversion function can pass in a parameter when a type conversion is made, the default conversion is 10, and a successful conversion returns a numeric value of the integer type.

Example: 1. parseint (' AB3 ', 16)//Return result: 2739, indicating the value of converting string to 16 binary

2.parseInt (' 13 ', 10)//Return Result: 13, value that converts string to 10 binary

3.parseInt (' 172 ', 8)//Return Result: 122, value that converts string to 8 binary

4.parseInt (' 0123 ', 4)//return Result: 27, value that converts string to 4 binary

5.parseInt (' 13 ', 6)//Return Result: 9, value that converts string to 6 binary

It is worth noting that:

(1) Conversion function is not only able to convert 2,8,16, he can be converted into arbitrary system;

(2) There is also at the time of conversion, if the first character of the string to be converted is not within the scope of the corresponding binary, the conversion fails, returning Nan;

For example: parseint (' A12 '), when converted to 10, starts with a not in the decimal range, and returns the result as Nan

parseint (' XAB1 ', 16), when converted to 16, X is not in the hexadecimal range and returns a result of Nan

(3) If the first character of the string is within the range of the corresponding binary, it can be converted until it encounters a character that is not in the appropriate range, stops the conversion, and returns the result

For example: parseint (' abx2 ', 16), when converted to 16, AB is in the range of 16 can be converted, encountered X is not in the range of 16, then the conversion stop, return the result of 171;

Parsefloat () is similar to the method of parseint ().

2. Forcing type conversions

Number (value)-converts the given value to a digit (which can be an integer or a floating point);

Number can only convert a string to a numeric value of 10, parsing the entire string, or returning a Nan result if one of the strings is not in the corresponding range

Note that number can pass in the Date object and returns the milliseconds from January 1, 1970 to the present.

Cases:

var today=new Date ();
Number (today);

The result returned is: 1469870945565

The difference between two methods of converting a string into a numeric value in JS

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.