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

Javascript

Javascript Problem Overview


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

parseInt(10, radixValue); 

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

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

Solution 4 - Javascript

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)

Solution 5 - Javascript

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

Solution 6 - Javascript

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.

Solution 7 - Javascript

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.

Solution 8 - Javascript

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

Solution 9 - Javascript

> 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn CooperView Question on Stackoverflow
Solution 1 - JavascriptJustin NiessnerView Answer on Stackoverflow
Solution 2 - JavascriptMichael BerkowskiView Answer on Stackoverflow
Solution 3 - JavascriptGeorge CumminsView Answer on Stackoverflow
Solution 4 - JavascriptMathias SchwarzView Answer on Stackoverflow
Solution 5 - JavascriptnmoliveiraView Answer on Stackoverflow
Solution 6 - JavascriptKonstantin SmolyaninView Answer on Stackoverflow
Solution 7 - JavascriptmishsxView Answer on Stackoverflow
Solution 8 - Javascriptdomaci_a_nasView Answer on Stackoverflow
Solution 9 - JavascriptJeremy SeekampView Answer on Stackoverflow