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

How to convert floating point number to integer in javascript

青灯夜游
青灯夜游Original
2021-11-19 16:09:347636browse

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);

How to convert floating point number to integer in javascript

Method 2: Using the bit operator "|"

console.log(0 | 123.45)

How to convert floating point number to integer in javascript

Method 3 : Use the bit operator "^"

console.log(0 ^ 123.45)

How to convert floating point number to integer in javascript

Method 4: Use the bit operator "~~"

console.log(~~ 123.45);
console.log(~~ 123.4545);

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!

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