JavaScript examples of numeric and string conversions

  • 2020-03-30 02:26:27
  • OfStack

1. Convert Numbers to strings

A. To convert a number to a string, simply add an empty string to it:
 
var n = 100; 
var n_as_string = n + ""; 

B. To make the number more explicitly converted to a String, use the String() function:
 
var string_value = String(number); 

C. Use the toString() method:
 
string_value = number.toString(); 

The toString() method of the Number object (the basic Number is converted to the Number object so that the method can be called) has an optional argument that specifies the cardinality of the conversion. If you do not specify this parameter, the conversion takes place in base 10. However, Numbers can also be converted according to other cardinals (Numbers between 2 and 36).
Such as:
 
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" 

The d. toFixed() method converts a number to a string and displays the specified number of digits after the decimal point. It doesn't use an exponential notation.
 
var n = 123456.789; 
n.toFixed(0); // "123457" 
n.toFixed(1); // "123456.79" 

E. toExponential () using index notation to give a number into a string, in front of the decimal point is 1 digit, the decimal point have specific figures.
 
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 whole integer part of the number, it USES an exponential notation.
 
var n = 123456.789; 
n.toPrecision(4); // "1.235e+5" 
n.toPrecision(7); // "123456.8" 

2. Convert a string to a number

A. A less tricky but obvious way to convert a string to a Number is to call the Number() constructor as a function:
 
var number = Number(string_value); 

B. ParseInt () intercepts only integers. If a string begins with "0x" or "0x", parseInt() parses it into a hexadecimal number.
 
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.
 
parseFloat("3.14 meters"); // Returns 3.14 

D. if parseInt() and parseFloat() cannot convert the specified string to a number, they return NaN:
 
parseInt(''eleven"); // Returns Nan 
parseFloat("$72.47"); // Returns NaN 

3 JavaScript integer method

A. Discard the decimals and keep the integers
ParseInt (5/2)

B. Round up, add 1 to the whole number if there are decimals

Math. Ceil (5/2)

C. Round down

Math. Floor (5/2)

D. Round

Math. Round (5/2)

Related articles: