Home > Web Front-end > JS Tutorial > How to convert floating point number to integer in javascript

How to convert floating point number to integer in javascript

青灯夜游
Release: 2021-11-19 16:10:05
Original
7702 people have browsed it

Conversion method: 1. Use the parseInt() function, the syntax "parseInt (floating point number)"; 2. Use the bit operator "|", the syntax "0 | floating point number"; 3. Use the bit operator "^", the syntax is "0 ^ floating point number"; 4. Use the bit operator "~~", the syntax is "~~floating point number".

How to convert floating point number to integer in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Javascript method of converting floating point numbers to integers

Method 1: Use parseInt() function

parseInt(13.5);
parseInt(22.5);
Copy after login

How to convert floating point number to integer in javascript

Method 2: Using the bit operator "|"

console.log(0 | 123.45)
Copy after login

How to convert floating point number to integer in javascript

Method 3 : Use the bit operator "^"

console.log(0 ^ 123.45)
Copy after login

How to convert floating point number to integer in javascript

Method 4: Use the bit operator "~~"

console.log(~~ 123.45);
console.log(~~ 123.4545);
Copy after login

How to convert floating point number to integer in javascript

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to convert floating point number to integer in javascript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template