1. Convert numbers to strings
a. To convert a number into a string, just add an empty string to it:
The code copy is as follows:
var n = 100;
var n_as_string = n + "";
b. To convert numbers to strings more explicitly, you can use the String() function:
The code copy is as follows:
var string_value = String(number);
c. Use the toString() method:
The code copy is as follows:
string_value = number.toString();
The number object's (the basic number is converted to a Number object so that this method can be called) has an optional parameter toString() method, which specifies the cardinality of the conversion. If this parameter is not specified, the conversion will be performed with 10 as the base. However, numbers can also be converted according to other cardinalities (numbers between 2 and 36).
For example:
The code copy is as follows:
var n = 17;
binary_string = n.toString(2); // Evaluates to "10001"
octal_string = "0" + n.toString(8); // Evaluates to "021"
hex_string = "0x" + n.toString(16); // Evaluates to "0x11"
d. toFixed() method converts a number into a string and displays the specified number of digits after the decimal point. It does not use exponential notation.
The code copy is as follows:
var n = 123456.789;
n.toFixed(0); // "123457"
n.toFixed(1); // "123456.79"
e. toExponential() uses exponential notation to convert a number into a string, with a decimal point preceded by a 1 digit and a specific digit after the decimal point.
The code copy is as follows:
var n = 123456.789;
n.toExponential(1); // "1.2e+5"
n.toExponential(3); // "1.235e+5"
f. toPrecision() uses the specified number of meaningful digits to display a number, and if the number of meaningful digits is not enough to display the entire integer part of the number, it uses exponential notation.
The code copy is as follows:
var n = 123456.789;
n.toPrecision(4); // "1.235e+5"
n.toPrecision(7); // "123456.8"
2. Convert string to number
a. A lack of skills in converting a string to a number, but it is clear and clear: call the Number() constructor as a function:
The code copy is as follows:
var number = Number(string_value);
b. parseInt() only intercepts integers. If a string starts with "0x" or "0X", parseInt() parse it into a hexadecimal number. parseInt() can even accept a parameter to specify that it is to be parsed. The cardinality of the number, the legal value is between 2 and 36.
The code copy is as follows:
parseInt("3 blind mice"); // Returns 3
parseInt("12.34"); // Returns 12
parseInt("0xFF"); // Returns 255
parseInt("11", 2); // Returns 3 (1 * 2 + 1)
parseInt("ff", 16); // Returns 255 (15 * 16 + 15)
parseInt("zz", 36); // Returns 1295 (35 * 36 + 35)
parseInt("077", 8); // Returns 63 (7 * 8 + 7)
parseInt("077", 10); // Returns 77 (7 * 10 + 7)
c. parseFloat() intercepts integer and floating point numbers.
The code copy is as follows:
parseFloat("3.14 meters"); // Returns 3.14
d. If parseInt() and parseFloat() cannot convert the specified string into a number, they will return NaN:
The code copy is as follows:
parseInt(''eleven"); // Returns Nan
parseFloat("$72.47"); // Returns NaN
3 JavaScript rounding method
a. Discard the decimal part and keep the integer part
parseInt(5/2)
b. Round upwards, add 1 to the integer part if there is a decimal.
Math.ceil(5/2)
c. Round down
Math.floor(5/2)
d. Rounding
Math.round(5/2)