Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How javascript converts a string to a number

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how javascript converts strings into numbers. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Conversion methods: 1, the use of "-", "*", "/", "%", "+", "-" and other operators; 2, the use of "Number (value)" statement; 3, the use of "parseInt (stringNum)" statement; 4, the use of "parseFloat (stringNum)" statement.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript converts a string to a number

Method 1: use operators such as -, *, /,%, +, -, etc.

JavaScript automatically converts strings to numbers, and to NaN for those that cannot. For example:

Alert ("30" / 5); / / division operation, the result is: 6alert ("15"-5); / / subtraction operation, the result is: 10alert ("20" * "a"); / / multiplication, the result is: NaNalert ("20"% "3"); / / modular operation, the result is: 2var num1 = "6"; var num2 = "6"; var num3 = "a"; alert (+ + num1) / / convert the string to a number and then perform the + + operation, the result is: 7alert (--num2); / / convert the string to a number and then perform the-- operation, the result is: 5alert (+ + num3); / / the string cannot be converted to a number, the result is: NaN

Method 2: use the Number () function

The Number () function can convert an argument to a number

The format is as follows:

Number (value)

Number () converts the parameter value as a whole, and when any part of the parameter value contains a symbol that cannot be converted to a number, the conversion fails, and NaN is returned at this time, otherwise the converted number is returned.

When Number () digitally converts parameters, it follows some rules:

If the parameter contains only numbers, it will be converted to a decimal number, ignoring leading zeros and leading spaces; if the number is preceded by -,-will be retained in the conversion result; if the number is preceded by +, the + sign will be deleted after conversion

If the parameter contains a valid floating-point number, it is converted to the corresponding floating-point number, ignoring the leading 0 and leading spaces; if the number is preceded by -,-will be retained in the conversion result; if the number is preceded by +, the + sign will be deleted after conversion

If the parameter contains a valid hexadecimal number, it is converted to a decimal number of the corresponding size

If the parameter is an empty string, it will be converted to 0

If the parameter is a Boolean, convert true to 1 and false to 0

If the parameter is null, it will be converted to 0

If the parameter is undefined, it will be converted to NaN

If the parameter is a Date object, it will be converted to the number of milliseconds from January 1, 1970 to the time the conversion was performed

If the argument is a function, an array object that contains more than two elements, and an object other than a Date object, it is converted to NaN

If special symbols or non-numeric characters other than spaces, +, and-are included before the parameters, or if special symbols or non-numeric characters including spaces, +, and-are included in the parameters, it is converted to NaN.

Example of conversion:

Alert (Number ("0010")); / / remove two leading zeros, the result is: 10alert (Number ("+ 010")); / / remove leading zeros and +, resulting in: 10alert (Number ("- 10")); / / retain the "-" sign after conversion, resulting in-10alert (Number ('')); / / empty string conversion result: 0alert (Number (true)) / / the conversion result of the Boolean true is: 1alert (Number (null)); / / the conversion result of the null value is: 0var d = new Date (); / / create a Date object alert (Number (d)); / / convert the Date object, resulting in 1970.1.1 to the number of milliseconds when the conversion is performed: 1511351635179alert (Number ("100px")) The / / parameter contains the character px that cannot be converted to a number, and the result is: NaNalert (Number ("10001")); / / contains a space in the parameter, so that the whole parameter cannot be converted, resulting in: NaNalert (Number ("100123")); / / the parameter contains "-", so that the whole parameter cannot be converted, and the result is: NaNvar a; / declare the variable alert (Number (a)) / / variable an is not assigned, so the value of an is undefined. The result of undefined conversion is: NaNvar fn = function () {alert (1);}; / / create a function object alert (Number (fn)); / / conversion function, the result is: NaNalert (Number (window)); / / convert window object, the result is: NaN

From the above example, we can also see that Number () is converted as a whole, and any place that contains illegal characters will cause the conversion to fail. Unlike Number (), the next two functions are converted bit by bit from left to right, stopping the conversion immediately when either bit cannot be converted, while returning the value that has been successfully converted.

Method 3: use the parseInt () function

The parseInt () function can convert an argument to an integer

The format is as follows:

ParseInt (stringNum, [radix])

The stringNum parameter is a string that needs to be converted to an integer; the radix parameter is a number between 2 and 36, indicating the binary number of the stringNum parameter. A value of 10:00 can be omitted.

The purpose of parseInt () is to parse the radix-based stringNum string parameter into a decimal number. If the stringNum string does not begin with a legal character, if an illegal character is encountered during NaN; parsing, the parsing will be stopped immediately and the parsed value will be returned.

ParseInt () follows the following rules when parsing strings to integers:

When parsing a string, the spaces before and after the string are ignored; if the string is preceded by -,-will be retained in the conversion result; if the number is preceded by +, the + sign will be deleted after conversion

If the string is preceded by a special symbol other than spaces, + and -, or a non-numeric character other than axif (or axif), the string will not be parsed and the result will be NaN

Contains spaces, +, -, and decimal points in the string. " When special symbols or non-numeric characters are encountered, parsing stops when these characters are encountered and returns the parsed result

If the string is an empty string, the result is NaN.

Example of conversion:

Alert (parseInt ("1101", 2)); / / the result of parsing a 1101 string with a base of 2 is: 13alert (parseInt ("a37f", 16)); / / the result of parsing a string of a37f with a base of 16 is: 41855alert (parseInt ("1101")); / / the result of parsing a string with a cardinality of 10 is: 123alert (parseInt ("123")) The space before the / / string is ignored. The result is: 123alert (parseInt ("123")); / / the string contains spaces, which stops when parsing to spaces, resulting in 12alert (parseInt (" 12.345 ")); / / the string contains the decimal point, which stops when parsing to the decimal point, resulting in 12alert (parseInt (" xy123 ")). The / / string contains the non-numeric character "x" in front of it and cannot be parsed. The returned result is: NaNalert (parseInt ("123xy4")); / / the string contains the non-numeric character "xy", which stops when parsing to "x". The result is 123.

From the above example, we can see that when parseInt () parses a floating point, the fractional part of the data is truncated, so you need to use parseFloat (), which will be described below, instead of parseInt ().

Method 4: use the parseFloat () function

The parseFloat () function can convert an argument to a floating point number

The format is as follows:

ParseFloat (stringNum)

The stringNum parameter is a string that needs to be resolved to a floating-point type.

The purpose of parseFloat () is to parse a string whose first digit is a number into a floating-point number. If the stringNum string does not begin with a legal character, if an illegal character is encountered during NaN; parsing, the parsing will be stopped immediately and the parsed value will be returned.

ParseFloat () follows the following rules when parsing strings to integers:

When parsing a string, the spaces before and after the string are ignored; if the string is preceded by -,-will be retained in the conversion result; if the number is preceded by +, the + sign will be deleted after conversion; if the string is preceded by a decimal point. The conversion result adds a 0 before the decimal point

If the string is preceded by a space, +, -, and. The string will not be parsed and the return result will be NaN

When a string contains special symbols or non-numeric characters such as spaces, +, and -, parsing stops when these characters are encountered and the parsed result is returned

When a string contains more than two decimal points, parsing stops when it reaches the second decimal point, and the parsed result is returned

If the string is an empty string, the result is NaN.

Example of conversion:

Alert (parseFloat ("312.456")); / / the result is: 312.456alert (parseFloat ("- 3.12")); / / the "-" before the string is retained, the result is:-3.12alert (parseFloat ("+ 3.12")); / / the "-" before the string is retained, resulting in: 3.12alert (parseFloat (".12")); / / adding 0 before the decimal point, the result is: 0.12alert (parseFloat ("3.12")) / / truncate the space in front of the string, the result is: 3.12alert (parseFloat ("312.4A56")); / / string contains non-numeric character A, parsing to A stops, the result is: 312.4alert (parseFloat ("312.4A56")); / / string contains spaces, stop when parsing to spaces, the result is: 31alert (parseFloat ("31.2.5")) / / the string contains two decimal points, which stops when parsing to the second decimal point. The result is: 31.2alert (parseFloat ("a312.456")) / / string in front of the non-numeric character a, parsing can not be carried out, the result is: NaN on "javascript how to convert a string into a number" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report