Home Web Front-end JS Tutorial JavaScript number and string conversion example_javascript tips

JavaScript number and string conversion example_javascript tips

May 16, 2016 pm 04:54 PM
string number Convert

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Practical tips for converting full-width English letters into half-width form Practical tips for converting full-width English letters into half-width form Mar 26, 2024 am 09:54 AM

Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

Detailed explanation of the implementation method of converting PHP months to English months Detailed explanation of the implementation method of converting PHP months to English months Mar 21, 2024 pm 06:45 PM

This article will introduce in detail how to convert months in PHP to English months, and give specific code examples. In PHP development, sometimes we need to convert digital months to English months, which is very practical in some date processing or data display scenarios. The implementation principles, specific code examples and precautions will be explained in detail below. 1. Implementation principle In PHP, you can convert digital months into English months by using the DateTime class and format method. Date

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to convert full-width English letters into half-width letters How to convert full-width English letters into half-width letters Mar 25, 2024 pm 02:45 PM

How to convert full-width English letters into half-width letters In daily life and work, sometimes we encounter situations where we need to convert full-width English letters into half-width letters, such as when entering computer passwords, editing documents, or designing layouts. Full-width English letters and numbers refer to characters with the same width as Chinese characters, while half-width English letters refer to characters with a narrower width. In actual operation, we need to master some simple methods to convert full-width English letters into half-width letters so that we can process text and numbers more conveniently. 1. Full-width English letters and half-width English letters

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

Quickly learn about ASCII value conversion in PHP Quickly learn about ASCII value conversion in PHP Mar 28, 2024 pm 06:42 PM

ASCII value conversion in PHP is a problem often encountered in programming. ASCII (American Standard Code for Information Interchange) is a standard encoding system for converting characters into numbers. In PHP, we often need to convert between characters and numbers through ASCII code. This article will introduce how to convert ASCII values ​​in PHP and give specific code examples. 1. Change the characters

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

See all articles