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

What are the methods of converting numbers by JavaScript strings

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

Share

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

This article mainly introduces "what are the methods of converting JavaScript strings into numbers". In daily operation, I believe that many people have doubts about the methods of converting numbers by JavaScript strings. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the methods of converting numbers by JavaScript strings?" Next, please follow the editor to study!

There are three main methods of js string conversion: conversion function, forced type conversion, and weak type conversion using js variables.

1. Conversion function:

Js provides two conversion functions, parseInt () and parseFloat (). The former converts a value to an integer, while the latter converts a value to a floating point number. These two functions work correctly only if these methods are called on the String type; NaN (Not a Number) is returned for the other types.

Some examples are as follows:

ParseInt ("1234blue"); / / returns 1234 parseInt ("0xA"); / / returns 10 parseInt ("22.5"); / / returns 22 parseInt ("blue"); / / returns NaN

The parseInt () method also has a base mode, which converts a binary, octal, hexadecimal, or any other string to an integer. The base is specified by the second parameter of the parseInt () method. The example is as follows:

ParseInt ("AF", 16); / / returns 175 parseInt ("10", 2); / / returns 2 parseInt ("10", 8); / / returns 8 parseInt ("10", 10); / / returns 10

If the decimal number contains a leading 0, it is best to use the cardinality of 10 so that you do not accidentally get an octal value. For example:

ParseInt ("010") / / returns 8

ParseInt ("010", 8); / / returns 8 parseInt ("010", 10); / / returns 10

The parseFloat () method is handled in a similar way to the parseInt () method. Another difference in using the parseFloat () method is that the string must represent a floating-point number in decimal form, and parseFloat () has no base pattern.

The following is an example of using the parseFloat () method:

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 NaN2. Forced type conversion

You can also use forced type conversions (type casting) to handle the types of converted values. Use cast to access a specific value, even if it is of another type. The three cast types available in ECMAScript are as follows:

Boolean (value)-converts a given value to a Boolean type

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

String (value)-converts a given value to a string.

Converting a value with one of these three functions creates a new value that holds the value converted directly from the original value. This will have unintended consequences. The Boolean () function returns true when the value to be converted is a string, a non-zero number, or an object with at least one character (as discussed in the next section). If the value is an empty string, the number 0, undefined, or null, it returns false.

Front-end Learning Autumn qun: 767273102. If you have anything you don't understand, feel free to ask me.

You can test the Boolean cast with the following code snippet.

Boolean (""); / / false-empty string Boolean ("hi"); / / true-non-empty string Boolean (100); / / true-non-zero number Boolean (null); / / false-null Boolean (0); / / false-zero Boolean (new Object ()); / / true-object

The cast of Number () is similar to that of the parseInt () and parseFloat () methods, except that it converts the entire value, not part of the value. Examples are as follows:

Usage results Number (false) 0 Number (true) 1 Number (undefined) NaN Number (null) 0 Number ("5.5") 5.5 Number ("56") 56 Number ("5.6.7") NaN Number (new Object ()) NaN Number

The last method of casting, String (), is the simplest, as shown in the following example:

Var S1 = String (null); / / "null" var oNull = null; var S2 = oNull.toString (); / / won't work, causes an error3. Weak type conversion using js variables

To give a small example, you will understand at a glance.

Var str= '012.345'; var x = str-0; x = x # 1; at this point, the study of "what are the methods of converting JavaScript strings into numbers" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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