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( )<括号里面的数字是几就转换成几进制>
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"
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)); // 四舍五入为指定小数位数的数字。
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(八进制计算)
谷歌正常 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 = 数字 + "";
字符串转数字:
var strs = + - * / %“数字”;
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!

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.