Home > Article > Web Front-end > javascript numerical conversion
JavaScript is an interpreted language that contains various types of data, including numbers, strings, Boolean values, objects, etc. When developing computing and logical processing applications, it involves processing, computing, converting and other operations on these data. This article will take an in-depth look at numeric conversion operations in JavaScript.
Commonly used data types in JavaScript
In JavaScript, the commonly used basic data types are the following:
Numeric types in JavaScript
In JavaScript, numeric types mainly refer to numeric type variables, including integers and floating point numbers. Numeric type variables can perform numerical operations using various arithmetic operators (such as , -, *, /, etc.). Here are examples of some common numeric types in JavaScript:
Because numeric types in JavaScript use the IEEE 754 standard to represent floating-point numbers, they may produce some strange behavior, especially when performing floating-point operations. For example:
console.log(0.1 0.2); // 0.30000000000000004
When comparing numeric type variables, you need to pay attention to some precision issues. You can use the toFixed() method to convert numbers. number of digits, for example:
console.log((0.1 0.2).toFixed(1)); // 0.3
Convert the string into a numeric type
in In JavaScript, you can use the Number() method to convert string type data into numeric type. For example:
var str = "123";
var num = Number(str);
console.log(typeof num); // number
required Note that if the string parameter cannot be converted to a number, NaN (Not a Number) will be returned. For example:
var str = "hello";
var num = Number(str);
console.log(num); // NaN
In addition, You can also use the parseInt() and parseFloat() methods to convert strings to integer and floating-point types. These two methods are typically used to read numeric values from user-entered forms:
var str = "123.45";
var num1 = parseInt(str);
var num2 = parseFloat( str);
console.log(num1); // 123
console.log(num2); // 123.45
It should be noted that the parseInt() method only returns data In the first integer part, parsing will stop when encountering non-numeric characters. For example:
var str = "123.45px";
var num = parseInt(str);
console.log(num); // 123
will Convert numbers to string types
In JavaScript, you can use the toString() method to convert numeric type data into string types. For example:
var num = 123;
var str = num.toString();
console.log(typeof str); // string
needs attention What is important is that the toString() method needs to be called on a numeric type variable, otherwise an error will be reported. For example:
var str = toString(123); // TypeError: toString is not a function
When splicing numeric type variables and string type variables, JavaScript will automatically convert the numeric type into Convert to string type. For example:
var num = 123;
var str = "hello ";
console.log(str num); // hello 123
Convert Boolean value Type conversion to numeric type
In JavaScript, you can use the Number() method to convert Boolean type data into a numeric type. true and false will be converted to 1 and 0 respectively. For example:
var bool = true;
var num = Number(bool);
console.log(num); // 1
You can also use multiplication Operator * converts a Boolean value into a numeric type variable. Both true and false are converted to 1 and 0 respectively. For example:
var bool = true;
var num = bool * 1;
console.log(num); // 1
Convert the numeric type to Boolean value type
In JavaScript, you can use the Boolean() method to convert numeric type data into a Boolean value type. The following values will be converted to false:
All other values (including the string "0") will be converted to true. For example:
console.log(Boolean(0)); // false
console.log(Boolean(-0)); // false
console.log(Boolean(0n)) ; // false
console.log(Boolean(NaN)); // false
console.log(Boolean(null)); // false
console.log(Boolean(undefined)); / / false
console.log(Boolean("")); // false
console.log(Boolean("hello" )); // true
var bool = !!num;
JavaScript contains various types of data, among which numeric type variables are commonly used. In JavaScript code, it is extremely common to convert data types. This article introduces the conversion between string type, Boolean value type and numeric type data. I hope this article can help readers better understand data type conversion in JavaScript.
The above is the detailed content of javascript numerical conversion. For more information, please follow other related articles on the PHP Chinese website!