Home  >  Article  >  Web Front-end  >  JavaScript number and string conversion example_javascript tips

JavaScript number and string conversion example_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:54:291328browse

1. Convert a number to a string

a. To convert a number to a string, just add an empty string to it:

Copy code The code 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:
Copy code The code is as follows:

var string_value = String(number);

c. Use toString() method:
Copy code The code is as follows:

string_value = number.toString();

Number object (basic number is converted to Number object, so that This method can be called) toString() method has an optional parameter, which is used to specify the base of the conversion. If this parameter is not specified, conversion will be performed in base 10. However, numbers can also be converted to other bases (numbers between 2 and 36).
For example:
Copy code The 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. The 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.
Copy code The code 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 to a string with a decimal point There is 1 digit in front and a specific number of digits after the decimal point.
Copy code The 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 number of meaningful digits to Displays a number, and if there are not enough meaningful digits to display the entire integer portion of the number, it uses exponential notation.
Copy code The code 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 less technical but very clear way to convert a string to a number is to call the Number() constructor as a function:
Copy code The code is as follows:

var number = Number(string_value);

b. parseInt() only intercepts integers, if it is a string Starting with "0x" or "0X", parseInt() parses it into a hexadecimal number. parseInt() can even accept a parameter to specify the base of the number to be parsed. The legal value is between 2 and 36. between.
Copy code The 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 code The 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 code The code is as follows:

parseInt(''eleven"); // Returns Nan
parseFloat("$72.47"); // Returns NaN

3 JavaScript rounding methods

a. Discard the decimal part and keep the integer part
parseInt(5/2)

b. Round up, if there is a decimal, use the integer part Add 1

Math.ceil(5/2)

c. Round down

Math.floor(5/2)

d. Rounding

Math.round(5/2)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn