Table of Contents
1)Number()
2)parseInt(): Returns an integer
3)parseFloat(): Returns a floating point number
4)toString(): Other types—>string
5)toFixed(n): Keep n decimal places (output type: string)
解释:
兼容:
数字——字符串: 数字 +(连字符)<数字+字符串=字符串>
字符串——数字: + - * / %
显示转换:
隐式转换:
数字转换为字符串:+ ""
字符串转数字:
Home Web Front-end JS Tutorial Specific analysis of data type conversion in JavaScript

Specific analysis of data type conversion in JavaScript

Aug 08, 2017 pm 01:50 PM
javascript js Convert


JavaScript data type conversion

Display conversion

There are three functions that can convert Convert string to numeric value: Number(), parseInt(), parseFloat(). Number() can be used for any data type, while the other two functions are specifically used to convert strings into numbers.

1)Number()

If the string is a numeric string, convert it to a decimal value.

If the string contains a valid hexadecimal format, such as "0xf", convert it to a decimal integer value of the same size.

If the string is an empty string or a space string, convert it to 0.

If the string contains characters other than those in the above format, convert it to NaN.

2)parseInt(): Returns an integer

When the parseInt() function converts a string, it depends more on whether it conforms to the numerical pattern. It ignores leading spaces in the string until it finds the first non-space character. If the first character is not a numeric character or a negative sign, parseInt() will return NaN. If it is an empty string, it will return NaN. If the first character is a numeric character, parseInt() will continue parsing the second character until all subsequent characters have been parsed or a non-numeric character is encountered.

3)parseFloat(): Returns a floating point number

parseFloat() can return decimals. When parseFloat() converts, it also parses each character starting from the first character until it parses to the end of the string, or until it encounters an invalid floating-point numeric character, that is, the first character in the string The decimal point is valid, but the second decimal point is invalid.

4)toString(): Other types—>string

Convert decimal to binary

Example:

var a = 302;
console.log(a.toString(2));
toString( )<括号里面的数字是几就转换成几进制>
Copy after login

Convert other types of variables to For strings, we usually use the toString() method. Numeric values, Boolean values, objects and strings all have toString() method, but null and undefined do not have this method. We can also use the conversion function String(), which can convert any type of value into a string. When toString can represent a string in binary, octal, hexadecimal, or even other valid base formats by inputting the base.

For example:

console.log(a.toString()); // "123"
console.log(bool.toString()); // "true"
Copy after login

5)toFixed(n): Keep n decimal places (output type: string)

var pi = 0.45;   //0.95对toFixed有疑问为0.9
alert(Math.round(pi));  //round四舍五入取整
alert(pi.toFixed(1));   // 四舍五入为指定小数位数的数字。
Copy after login

Output type: string

解释:

toFixed它是一个四舍六入五成双的诡异的方法,"四舍六入五成双"含义:对于位数很多的近似数,当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,这种修约(舍入)规则是“四舍六入五成双”,也即“4舍6入5凑偶”这里“四”是指≤4 时舍去,"六"是指≥6时进上,"五"指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:①5前为奇数,舍5不进;②5前为偶数,舍5入1。

兼容:

var a = "070";
alert(typeof parseInt(a));
alert(parseInt(a));
IE6-7-8:56(八进制计算)
Copy after login

谷歌正常 70;

处理兼容:(a, 10),强制几进制输出!

隐式转换

隐式转换的方式可以通过+""、* 1、/ 1的运算来把数值转换成字符串。(可相互)

数字——字符串: 数字 +(连字符)<数字+字符串=字符串>

字符串——数字: + - * / %

"123"-0 "123"*0 "123"/0 +"123" "123"%比前面的数值字符串大的数字;

显式转换是通过函数的形式,将字符串转换成数组的形式,隐式转换则是通过运算符的方式将字符串转换成数字的形式。

总结:

显示转换:

1)Number()

2)parseInt():返回整数

3)parseFloat():返回浮点数

4)toString():其它类型—>字符串

5)toFixed(n):保留n位小数(输出类型:string)

隐式转换:

数字转换为字符串:+ ""

var str =  数字 + "";
Copy after login

字符串转数字:

var strs =  + - * / %“数字”;
Copy after login

The above is the detailed content of Specific analysis of data type conversion in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to convert ODT to Word in Windows 11/10? How to convert ODT to Word in Windows 11/10? Feb 20, 2024 pm 12:21 PM

In this article, we will show you how to convert OpenDocumentTextDocument (ODT) files to Microsoft Word (Docx, DOC, etc.). Format. How to Convert ODT to Word in Windows 11/10 Here is how you can convert ODT documents to DOC or DOCX format on Windows PC: Convert ODT to Word using WordPad or Word The first method we are going to show you Is to use WordPad or MicrosoftWord to convert ODT to Word. Here are the steps to achieve this: First, open the WordPad app using the Start menu. Now, go to

How to convert AI files to CDR format How to convert AI files to CDR format Feb 19, 2024 pm 04:09 PM

AI files refer to vector graphics files created by Adobe Illustrator (AI for short) software, while CDR files refer to vector graphics files created by CorelDRAW software. Since these two softwares are developed by different manufacturers, their file formats are different and cannot be directly converted to each other. However, we can convert AI files to CDR files through some methods. A commonly used conversion method will be introduced below. Step 1: Export AI files to EPS format AdobeIllust

How to convert a virtual machine to a physical machine? How to convert a virtual machine to a physical machine? Feb 19, 2024 am 11:40 AM

Converting a virtual machine (VM) to a physical machine is the process of migrating a virtual instance and associated application software to a physical hardware platform. This conversion helps optimize operating system performance and hardware resource utilization. This article aims to provide an in-depth look at how to make this conversion. How to implement migration from virtual machine to physical machine? Typically, the conversion process between a virtual machine and a physical machine is performed outside the virtual machine by third-party software. This process consists of multiple stages involving the configuration of virtual machines and the transfer of resources. Prepare the physical machine: The first step is to ensure that the physical machine meets the hardware requirements for Windows. We need to back up the data on a physical machine as the conversion process will overwrite the existing data. *Username and password for an administrator account with administrator rights to create system images. will be virtual

Golang time processing: How to convert timestamp to string in Golang Golang time processing: How to convert timestamp to string in Golang Feb 24, 2024 pm 10:42 PM

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

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 convert qq music to mp3 format Convert qq music to mp3 format on mobile phone How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone Mar 21, 2024 pm 01:21 PM

QQ Music allows everyone to enjoy watching movies and relieve boredom. You can use this software every day to easily satisfy your needs. A large number of high-quality songs are available for everyone to listen to. You can also download and save them. The next time you listen to them, you don’t need an Internet connection. The songs downloaded here are not in MP3 format and cannot be used on other platforms. After the membership songs expire, there is no way to listen to them again. Therefore, many friends want to convert the songs into MP3 format. Here, the editor explains You provide methods so that everyone can use them! 1. Open QQ Music on your computer, click the [Main Menu] button in the upper right corner, click [Audio Transcoding], select the [Add Song] option, and add the songs that need to be converted; 2. After adding the songs, click to select Convert to [mp3]

See all articles