开发者

Why do we need to use radix parameter when calling parseInt?

开发者 https://www.devze.com 2023-03-19 02:20 出处:网络
What does radix actually means? Why do we need it? parseInt(10, radi开发者_JAVA技巧xValue); You might not always want to parse the integer into a base 10 number, so supplying the radix allows you t

What does radix actually means? Why do we need it?

parseInt(10, radi开发者_JAVA技巧xValue); 


You might not always want to parse the integer into a base 10 number, so supplying the radix allows you to specify other number systems.

The radix is the number of values for a single digit. Hexidecimal would be 16. Octal would be 8, Binary would be 2, and so on...

In the parseInt() function, there are several things you can do to hint at the radix without supplying it. These can also work against you if the user is entering a string that matches one of the rules but doesn't expressly mean to. For example:

// Numbers with a leading 0 used a radix of 8 (octal) before ECMAScript 5.
// These days, browsers will treat '0101' as decimal.
var result = parseInt('0101');

// Numbers that start with 0x use a radix of 16 (hexidecimal)
var result = parseInt('0x0101');

// Numbers starting with anything else assumes a radix of 10
var result = parseInt('101');

// Or you can specify the radix, in this case 2 (binary)
var result = parseInt('0101', 2);


Because if you have a string number like 0700 and want the output to be integer 700 you need to inform parseInt() that it is a decimal number rather than octal.

console.log(parseInt("0700"));
// 448

// I really wanted decimal (base 10)
console.log(parseInt("0700", 10));
// 700

// What is this? Binary, Decimal, Octal?
console.log(parseInt("0110"));
// 72

// as binary
console.log(parseInt("0110", 2));
// 6
Note I only answered half your question. See others for good definitions of what a radix actually is.


Radix is the base of a system of numeration. There are an infinite number of numeric systems but the ones with which most people are familiar are base 10 (decimal) and base 2 (binary).

Numeric values can be interpreted differently in different bases. For example, the number 10 in binary can be represented as 2 in decimal.

In the case of parseInt(), the radix allows you to specify the base to be used. By default, a radix of 10 is used.

However, the radix should always be specified, even when using base 10. Consider the case of

parseInt("010") // Returns 8

At first glance, you may expect the statement to return 10. Explicit use of the radix will help to avoid confusion:

parseInt("010", 10) // Returns: 10


The radix is the base number of the number system: http://en.wikipedia.org/wiki/Radix

Normally, you only need to specify the radix if you want it to be different from 10. More specifically (from http://www.w3schools.com/jsref/jsref_parseInt.asp) :

If the radix parameter is omitted, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal) If the string begins with "0", the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal)


What is radix in paseInt() ?

The parseInt() function parses a string and returns an integer. It takes a second argument for the radix, which specifies the base of the number in the string. The radix can be an integer between 2 and 36.

The function call looks like (Syntax):

parseInt(string, radix);

Some examples to clarify the concept of radix

Example 1:

var a = parseInt("11", 2);

The radix variable says that "11" is in the binary system, or base 2. Therefore, this example converts the string "11" to an integer 3.

Example 2:

var a = parseInt("10011", 16);

Here the radix tells the parseInt() that 10011 is a hexadecimal number and hence in integer, it gets converted to 65553

Basically, long story short, the radix argument tells the parseInt() that the string passed as 1st parameter is of a particular system (binary, hexadecimal etc) and it needs to be converted into an integer as an end product.


Just adding some additional information which clearly answers the question:

If radix is undefined or 0 (or absent), JavaScript assumes the following:

  • [...]
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
  • [...]

Source: MDN parseInt()


It's just my opinion but idea that "we need to use radix" quickly becomes outdated. The problem really was actual some time ago because people outside IT world usually don't use number notations other than decimal and quite often provide decimal numbers zero-padded like "010". But since ECMAScript 6 octal numbers in JS are prefixed by "0o" and not just "0" as it was in ECMAScript 5 and 3. So if you don't target IE family (that is not rare situation now) you can skip radix safely.


I learned the hard way that you always need to provide radix when working with parseInt in 2011. but in modern browsers, it seems that they "fixed" it.

If you run a function on a string starting with 0, it will parse it with radix 10. But if you run it on a number for whatever reason, then it will still use radix 8, as mentioned in MDN.

console.log(parseInt('015'));
// This returns 15

console.log(parseInt(015));
// This returns 13


The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal)

If the string begins with "0", the radix is 8 (octal). This feature is deprecated

If the string begins with any other value, the radix is 10 (decimal)

Source W3Schools

0

精彩评论

暂无评论...
验证码 换一张
取 消