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

Use button trigger events to achieve background color flashing effect

To achieve the background color flashing effect, j...

Vue song progress bar sample code

Note that this is not a project created by vue-cl...

Native JS realizes compound motion of various motions

This article shares with you a compound motion im...

Detailed explanation of mysql record time-consuming sql example

mysql records time-consuming sql MySQL can record...

Implementation of scheduled backup in Mysql5.7

1. Find mysqldump.exe in the MySQL installation p...

How to install PostgreSQL11 on CentOS7

Install PostgreSQL 11 on CentOS 7 PostgreSQL: The...

A "classic" pitfall of MySQL UPDATE statement

Table of contents 1. Problematic SQL statements S...

Analysis of GTK treeview principle and usage

The GtkTreeView component is an advanced componen...

MySQL database advanced query and multi-table query

MySQL multi-table query Add a worksheet -- User t...

An example of the calculation function calc in CSS in website layout

calc is a function in CSS that is used to calcula...

Implementation of docker view container log command

Why should we read the log? For example, if the c...

Analyze the usage and principles of Vue's provide and inject

First, let's talk about why we use provide/in...

How to use Maxwell to synchronize MySQL data in real time

Table of contents About Maxwell Configuration and...