


How to convert numerical values in javascript? Summary of methods for javascript numerical conversion
The content of this article is about how to convert numerical values in JavaScript? The summary of JavaScript numerical conversion methods has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The following three functions convert non-numeric values into numeric values: Number(), parseInt() and parseFloat()
Conversion rules for Number()
Boolean The value true will be converted to 1 and false will be converted to 0
var correct = true Number(correct) 1 var error = false Number(error) 0
What numeric value is passed in will be returned
var sales = 10 Number(sales) 10
null value, return 0
var arg = null Number(arg) 0
undefined, return NaN
var vara = undefined Number(vara) NaN
String, follow the following rules:
If the string contains only numbers (including the case with a positive or negative sign in front), it will be converted into a decimal value, that is, " 1"
will become 1, "123" will become 123, and "011" will become 11 (note: leading zeros are ignored)
var str = '1' Number(str) 1 var str1 = '011' Number(str1) 11 var str2 = '+1' Number(str2) 1 var str3 = '-001' Number(str3) -1 var str4 = '-121' Number(str4) -121
If the string contains a valid Floating point format, such as "1.1", convert it to the corresponding floating point value (similarly,
leading zeros will also be ignored)
var str = '1.1' Number(str) 1.1 var str1 = '+1.1' Number(str1) 1.1 var str2 = '-01.1' Number(str2) -1.1
If the string contains valid hexadecimal format, such as "0xf", then convert it to a decimal integer of the same size
Numerical value
If the string is empty (does not contain any characters), then convert it to 0
var str = '' Number(str) 0
If the string contains characters other than the above format, convert it to NaN
var str = 'adfsfdsa' Number(str) NaN
If it is an object, call the valueOf() method of the object, and then follow The previous rule transforms the returned value. If the result of the conversion is NaN, call the object's toString() method, and then convert the returned string value again according to the previous rules
parseInt() conversion rules
Since the Number() function is more complicated and unreasonable when converting strings, the
parseInt() function is more commonly used when processing integers. When the parseInt() function converts a string, it depends more on whether it conforms to the numerical pattern. It ignores spaces preceding the
string until it finds the first non-space character. If the first character is not a numeric character, parseInt()
will return NaN; that is, using parseInt() to convert an empty string will return NaN (Number() returns 0 for null characters). If
the first character is a numeric character, parseInt() will continue to parse the second character until all subsequent characters are parsed or
a non-numeric character is encountered. For example, "1234blue" will be converted to 1234, because "blue" will be completely ignored. Similarly, "22.5"
will be converted to 22 because the decimal point is not a valid numeric character.
指定基数会影响到转换的输出结果。例如: var num1 = parseInt("10", 2); //2 (按二进制解析) var num2 = parseInt("10", 8); //8 (按八进制解析) var num3 = parseInt("10", 10); //10 (按十进制解析) var num4 = parseInt("10", 16); //16 (按十六进制解析)
**多数情况下,我们要解析的都是十进制数值,因此始终将 10 作为第二个参数是 非常必要的。**
parseFloat() conversion rules
Similar to the parseInt() function, parseFloat() also parses each character starting from the first character (position 0). And
is also parsed until the end of the string, or until an invalid floating-point numeric character is encountered. In other words, the
th decimal point in the string is valid, but the second decimal point is invalid, so the string after it will be ignored. For example,
"22.34.5" will be converted to 22.34.
In addition to the valid first decimal point, the second difference between parseFloat() and parseInt() is that it always ignores leading
zeros. parseFloat() recognizes all floating-point numerical formats discussed previously, including decimal integer formats. However, strings in the hexadecimal format
will always be converted to 0. Since parseFloat() only parses decimal values, it has no use for specifying the base number with the second parameter. One last thing to note: if the string contains a number that can be parsed as an integer (no decimal point, or
after the decimal point are all zeros), parseFloat() will return an integer.
var num1 = parseFloat("1234blue"); //1234 (整数) var num2 = parseFloat("0xA"); //0 var num3 = parseFloat("22.5"); //22.5 var num4 = parseFloat("22.34.5"); //22.34 var num5 = parseFloat("0908.5"); //908.5 var num6 = parseFloat("3.125e7"); //31250000
JavaScript Exploration: Using parseInt() for numerical conversion
The above is the detailed content of How to convert numerical values in javascript? Summary of methods for javascript numerical conversion. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
