search
HomeCommon ProblemWhat are the cast rules?

What are the cast rules?

Aug 21, 2020 pm 02:05 PM
cast

The forced type conversion rules are: 1. Convert characters to numerical values. [parseInt()] converts from left to right once. If it can be converted, it will be converted. If it cannot be converted, it will stop. [Math.round()] is strictly converted and cannot be converted. Any non-numeric characters are allowed; 2. Convert numerical values ​​to characters, [toString()] directly converts.

What are the cast rules?

The forced type conversion rule is:

Character to value

parseInt();Convert from left to right once. If it can be turned, it will be turned, and if it cannot be turned, it will stop; if it is the first place, it cannot be turned, and it will be NaN directly; the decimal point will not be recognized.

parseFloat();Equivalent to parseInt, and can recognize decimal points

Math.round();Strict conversion, no non-standard conversion is allowed Numeric characters, otherwise NaN; take the nearest integer

Number();Strict conversion, no non-numeric characters are allowed, otherwise NaN; direct conversion

var str = "123";

var str = "123abc";

var str = "123abc456";

var str = "a123";

var str = "adasd";

var str = "123.45";

var n = parseInt(str);

console.log(str);

console.log(typeof str);

console.log(n);

console.log(typeof n);

var str = "a567.892";

var n = parseFloat(str);

console.log(str);

console.log(typeof str);

console.log(n);

console.log(typeof n);

var str = "-456.789";

var n = Math.round (str);

console.log(str);

console.log(typeof str);

console.log(n);

console.log(typeof n);

var str = "-456.789a";

var n = Number(str);

console.log(str);

console.log(typeof str);

console.log(n);

console.log(typeof n);

Numeric value to character

toString();Direct conversion is equivalent to adding quotation marks to the value to be converted

Keep n as a decimal

toFixed();While adding quotation marks, round to n decimal places. If it is not enough, add zeros

var n = 10.3543;

var s = n.toString( );

console.log(n);

console.log(typeof n);

console.log(s);

console. log(typeof s);

var n = 10;

var s = n.toFixed(2);

console.log(n);

console.log(typeof n);

console.log(s);

console.log(typeof s);

console.log(123.567000000)

Numeric value to character

var n = 123;

var s = n "";

console.log(s)

Character to value

var s = "123";

var n = s - 0;

console.log (n)

Other rotation values

true is 1, false is 0

console.log(1 true); //2

console.log(1 false); //1

console.log(1 undefined); //NaN

console.log(1 NaN); //NaN

console.log(1 null); //1

Related learning recommendations: Programming video

The above is the detailed content of What are the cast rules?. For more information, please follow other related articles on the PHP Chinese website!

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

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

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.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)