1. convert numbers to strings
A. to convert a number into a string, just add an empty string to it:
Copy codeThe Code is as follows:
Var n = 100;
Var n_as_string = n + "";
B. To explicitly convert a number to a String, you can use the String () function:
Copy codeThe Code is as follows:
Var string_value = String (number );
C. Use the toString () method:
Copy codeThe Code is as follows:
String_value = number. toString ();
The toString () method has an optional parameter, which is used to specify the conversion base. If this parameter is not specified, the conversion is based on 10. However, you can also convert numbers based on other bases (numbers between 2 and 36.
For example:
Copy codeThe Code 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 to a string and displays the specified number of digits after the decimal point. It does not use exponential notation.
Copy codeThe Code is as follows:
Var n = 123456.789;
N. toFixed (0); // "123457"
N. toFixed (1); // "123456.79"
E. toExponential () converts a number into a string using exponential notation. There is one digit before the decimal point, and a specific digit after the decimal point.
Copy codeThe Code is as follows:
Var n = 123456.789;
N. toExponential (1); // "1.2e + 5"
N. toExponential (3); // "1.235e + 5"
F. toPrecision () uses the specified meaningful digits to display a number. If the meaningful digits are not enough to show the entire integer part of the number, it uses exponential notation.
Copy codeThe Code is as follows:
Var n = 123456.789;
N. toPrecision (4); // "1.235e + 5"
N. toPrecision (7); // "123456.8"
2. convert a string to a number
A. a method to convert a string to a Number that lacks some skills but is clear: Call the Number () constructor as a function:
Copy codeThe Code is as follows:
Var number = Number (string_value );
B. parseInt () Only truncates integers. If a string starts with "0x" or "0X", parseInt () resolves it into a hexadecimal number, parseInt () you can even use a parameter to specify the base number of The number to be parsed. The valid value ranges from 2 to 36.
Copy codeThe Code 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 integers and floating-point numbers.
Copy codeThe Code is as follows:
ParseFloat ("3.14 meters"); // Returns 3.14
D. If parseInt () and parseFloat () cannot convert the specified string to a number, they will return NaN:
Copy codeThe Code is as follows:
ParseInt (''eleven "); // Returns Nan
ParseFloat ("$72.47"); // Returns NaN
3. JavaScript Rounding Method
A. Discard the fractional part and retain the integer part.
ParseInt (5/2)
B. rounded up. Add 1 to the integer if there is a decimal number.
Math. ceil (5/2)
C. Round down
Math. floor (5/2)
D. Rounding
Math. round (5/2)