search
HomeWeb Front-endJS TutorialJavaScript number and string conversion example_javascript tips

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
php怎么将16进制字符串转为数字php怎么将16进制字符串转为数字Oct 26, 2021 pm 06:36 PM

php将16进制字符串转为数字的方法:1、使用hexdec()函数,语法“hexdec(十六进制字符串)”;2、使用base_convert()函数,语法“bindec(十六进制字符串, 16, 10)”。

iOS 17:如何在待机模式下更改iPhone时钟样式iOS 17:如何在待机模式下更改iPhone时钟样式Sep 10, 2023 pm 09:21 PM

待机是一种锁定屏幕模式,当iPhone插入充电器并以水平(或横向)方向定位时激活。它由三个不同的屏幕组成,其中一个是全屏时间显示。继续阅读以了解如何更改时钟的样式。StandBy的第三个屏幕显示各种主题的时间和日期,您可以垂直滑动。某些主题还会显示其他信息,例如温度或下一个闹钟。如果您按住任何时钟,则可以在不同的主题之间切换,包括数字、模拟、世界、太阳能和浮动。Float以可自定义的颜色以大气泡数字显示时间,Solar具有更多标准字体,具有不同颜色的太阳耀斑设计,而World则通过突出显示世界地

笔记本电脑打不出1-9数字怎么办笔记本电脑打不出1-9数字怎么办Feb 23, 2023 pm 05:19 PM

笔记本电脑打不出1-9数字是设置问题导致的,其解决办法:1、按下“win+r”打开运行输入cmd并回车;2、在命令提示符界面,输入osk并回车;3、点击虚拟键盘上的“选项”,并勾选“打开数字小键盘”;4、启动“numlock键”即可。

JavaScript中生成随机数字和字符串JavaScript中生成随机数字和字符串Sep 02, 2023 am 08:57 AM

生成随机数或字母数字字符串的能力在许多情况下都会派上用场。您可以使用它在游戏中的不同位置生成敌人或食物。您还可以使用它向用户建议随机密码或创建文件名来保存文件。我写了一篇关于如何在PHP中生成随机字母数字字符串的教程。我在这篇文章的开头说,几乎没有事件是真正随机的,同样的情况也适用于随机数或字符串生成。在本教程中,我将向您展示如何在JavaScript中生成伪随机字母数字字符串。在JavaScript中生成随机数让我们从生成随机数开始。我想到的第一个方法是Math.random(),它返回一个浮

C++程序将一个数字四舍五入到n位小数C++程序将一个数字四舍五入到n位小数Sep 12, 2023 pm 05:13 PM

在任何语言中编写程序时,将数字表示为输出是一项有趣且重要的任务。对于整数类型(short、long或medium类型的数据),很容易将数字表示为输出。对于浮点数(float或double类型),有时我们需要将其四舍五入到特定的小数位数。例如,如果我们想将52.24568表示为三位小数,需要进行一些预处理。在本文中,我们将介绍几种技术,通过四舍五入将浮点数表示为特定的小数位数。在不同的方法中,使用类似C的格式化字符串、使用精度参数以及使用数学库中的round()函数是很重要的。让我们逐个来看。带有

使用C++编写代码,找到第N个非平方数使用C++编写代码,找到第N个非平方数Aug 30, 2023 pm 10:41 PM

我们都知道不是任何数字的平方的数字,如2、3、5、7、8等。非平方数有N个,不可能知道每个数字。因此,在本文中,我们将解释有关无平方数或非平方数的所有内容,以及在C++中查找第N个非平方数的方法。第N个非平方数如果一个数是整数的平方,则该数被称为完全平方数。完全平方数的一些例子是-1issquareof14issquareof29issquareof316issquareof425issquareof5如果一个数不是任何整数的平方,则该数被称为非平方数。例如,前15个非平方数是-2,3,5,6,

在PHP中使用is_numeric()函数检查是否为数字在PHP中使用is_numeric()函数检查是否为数字Jun 27, 2023 pm 05:00 PM

在PHP编程语言中,is_numeric()函数是一种非常常用的函数,用于判断一个变量或值是否为数字。在实际编程中,经常需要对用户输入的数值进行验证,判断其是否为数字类型,这时就可以使用is_numeric()函数进行判断。一、is_numeric()函数简介is_numeric()函数是一个用于检测变量或值是否为数字的函数。如果变量或值为数字,则返回tru

Java中的数字(带有0前缀和字符串)Java中的数字(带有0前缀和字符串)Aug 29, 2023 pm 01:45 PM

Java中的数字重要的是要理解数字类不是一个有形的类,而是一个抽象的类。在它内部,我们有一组定义其功能的包装类。这些包装类包括Integer、Byte、Double、Short、Float和Long。您可能会注意到,这些与我们之前讨论的基本数据类型相同,但它们表示为具有大写名称的单独类,以符合类命名约定。根据特定函数或程序范围的要求,编译器自动将原始数据类型转换为对象,反之亦然,并且数字类是java.lang包的一部分。此过程称为自动装箱和拆箱。通过掌握数字类及其对应的包装类的抽象性质,我们可以

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software