Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of ParseInt () in js

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of ParseInt () in js, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

ParseInt () is a built-in JS function that parses integers in numeric strings. For example, parse the numeric string '100percent:

Const number = parseInt ('100'); number; / / 100

As expected, '100' is resolved to the integer 100.

ParseInt (numericalString, radix) also accepts the second parameter: from 2 to 36, which represents the cardinality of the string. For example, specifying 16 means that the parsed value is a hexadecimal number. Note that 10 is not the default value, and the most common ones are 2, 8, 10, and 16.

For example, we use parseInt to parse numeric strings in a binary way:

Const number = parseInt ('100mm, 2); number; / / 4

The binary number corresponding to 100 is 4, so 4 is returned.

1. The strange behavior of parseInt ()

ParseInt (numericalString) always converts its first parameter to a string (if not a string), and then parses the numeric string to an integer value.

This is why you can (but should not) use parseInt () to extract the integer portion of a floating point number:

ParseInt (0.005); / / = > 0 parseInt (0.005); / / = > 0 parseInt (0.0005); / / = > 0 parseInt (0.00005); / / = > 0 parseInt (0.000005); / / = > 0

Extract the integer portion of a floating-point number, such as 0.5, 0.05, and so on, and the result is 0, as expected.

How about extracting the integer part of 0.0000005?

ParseInt (0.0000005); / / = > 5

ParseInt () parses the floating point number 0.0000005 to 5. Why does parseInt (0.0000005) have such a strange behavior?

two。 Solve the weird behavior of parseInt ()

Let's take a look at the first parameter of parseInt (numericalString): if it's not a string, convert it to a string, then parse it, and return the parsed integer.

This may be the first clue.

Then, we try to manually convert the floating point number to a string representation:

String; / / > '0.5' String (0.005); / / >' 0.05' String (0.005); / / > '0.005' String (0.0005); / / >' 0.0005' String (0.00005); / / > '0.00005' String (0.000005); / / >' 0.000005' String (0.0000005); / / > '5eMur7'

The behavior of explicitly converting to a string (0.0000005) string is different from other floating-point numbers: it is expressed exponentially.

This is the second important clue!

When the exponential symbol is parsed to an integer, we get the number 5.

ParseInt (0.0000005); / / = > 5 / / same as parseInt (5e-7); / / = > 5 / / same as parseInt ('5e Murray 7'); / / = > 5

ParseInt ('5eMuo7') considers the first number' 5times', but skips' eMur7'.

The mystery has been solved! Because parseInt () always converts its first argument to a string, floating-point numbers that are less than 10 minus 6 will be expressed exponentially. ParseInt () then extracts the integer from the exponential representation of float.

In addition, to safely extract the integer portion of a floating-point number, it is recommended that you use the Math.floor () function:

Math.floor (0.005); / / = > 0 Math.floor (0.005); / / = > 0 Math.floor (0.0005); / / = > 0 Math.floor (0.00005); / / = > 0 Math.floor (0.000005); / / = > 0 Math.floor (0.0000005) / / = > 0 Thank you for reading this article carefully. I hope the article "sample Analysis of ParseInt () in js" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report