Exploration and correction of the weird behavior of parseInt() in js

Exploration and correction of the weird behavior of parseInt() in js

Background:

I wonder if you have noticed that if parseInt(0.006) or parseInt(0.0006) returns a value of 0, but parseInt(0.0000006) returns a strange value of 6. Why is this?

What is the parseInt function?

First of all, what is the function of parseInt()? It is a native method of js, which is used to convert a numeric string into a number of type Number, and it only converts the integer part.

parse means conversion, Int is an integer, the purpose is to convert it into an integer

var num = parseInt(demo ,radix); //demo is the number you want to convert

Passing a parameter

var demo = "123";
var num = parseInt(demo);
console.log(typeof(num) + ":" + num); //number: 123

var demo = true;
var num = parseInt(demo);
console.log(typeof(num) + ":" + num); //number: NaN

var demo = false;
var num = parseInt(demo);
console.log(typeof(num) + ":" + num); //number: NaN

var demo = 123.9;
var num = parseInt(demo);
console.log(typeof(num) + ":" + num); //number: 123, directly cut off the decimals without rounding var demo = "10";
var num = parseInt(demo, 16);
console.log(typeof(num) + ":" + num); //number: 16

Usage of passing two parameters

var num = parseInt(demo, radix); //radix means base

Explanation: When radix is ​​written as 16, the system will think that it is based on hexadecimal. 10 (zero) is zero in hexadecimal, which is based on hexadecimal. Convert it to decimal (that is, 16). The above is based on the target base and converted to decimal (radix range is 2-36)

example:

//1.
parseInt(10,2) //The result is 2 because 10 is considered as binary and converted to decimal to get 2

//2
var demo = "123abc";
var num = parseInt(demo);
console.log(typeof(num) + “:” + num); //Result: number: 123 
//Because parseInt starts from the numeric class and stops at the non-numeric class, returning the original number

Exploring the causes

We found that our ideal goal is to convert all decimals to 0, but parseInt (0.0000006) returns 6, which is outrageous.

console.log(parseInt(0.006)) //0
console.log(parseInt(0.0006)) //0
console.log(parseInt(0.00006)) //0
console.log(parseInt(0.000006)) //0
console.log(parseInt(0.0000006)) //6

When I returned, I began to explore the reasons and where the problem occurred.

parseInt(0.0000006) //0.000006
String(0.0000006); //6e-7
parseInt(6e-7); //6
parseInt(0.0000006) //6

Summary of reasons

Now I found the reason. It turns out that after 10 to the power of -6, the form changes to a complex number (6e-7). Then use parseInt() to return the 6 in 6e-7, so the final returned value is 6.

correct

If you want to return the decimal safely, you have to use another method!

Math.floor(0.00006); //0
Math.floor(0.000006); //0
Math.floor(0.0000006); //0

This way you can get the value you want.

Summarize

This is the end of this article about the strange behavior of parseInt() in js. For more information about parseInt() in js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of JS conversion functions Number(), parseInt(), parseFloat()
  • In-depth understanding of Number(), parseInt(), and parseFloat() in JS
  • Detailed explanation of the difference between Number(), parseInt() and parseFloat() in js
  • Analysis of parseInt() and map() usage in JS
  • Definition and usage analysis of parseInt() function in javascript
  • Javascript function parseInt() has a bug when converting
  • Introduction to the use of the parseInt() method of the global object in javascript
  • JavaScript parseInt() function base conversion attention details
  • A brief discussion on the magic of parseInt() in JavaScript

<<:  Detailed explanation of the use of MySQL group links

>>:  Detailed explanation of scheduled tasks for ordinary users in Linux

Recommend

How to install jupyter in docker on centos and open ports

Table of contents Install jupyter Docker port map...

What are the attributes of the JSscript tag

What are the attributes of the JS script tag: cha...

Detailed explanation of Tomcat directory structure

Table of contents Directory Structure bin directo...

MySQL 5.7.17 compressed package installation-free configuration process diagram

There are two versions of MySQL database manageme...

Shorten the page rendering time to make the page run faster

How to shorten the page rendering time on the bro...

HTML imitates Baidu Encyclopedia navigation drop-down menu function

HTML imitates the Baidu Encyclopedia navigation d...

A brief discussion on event-driven development in JS and Nodejs

Table of contents Event-driven and publish-subscr...

jQuery implements dynamic tag event

This article shares the specific code of jQuery t...

Inspiring Design Examples of Glossy and Shiny Website Design

This collection showcases a number of outstanding ...

Detailed explanation of the mysql database LIKE operator in python

The LIKE operator is used in the WHERE clause to ...

How to reduce the root directory of XFS partition format in Linux

Table of contents Preface System environment Curr...

Detailed examples of how to use the box-shadow property in CSS3

There are many attributes in CSS. Some attributes...

Detailed explanation of Nginx access restriction configuration

What is Nginx access restriction configuration Ng...