What does radix actually means? Why do we need it?
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...
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 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)
Just adding some additional information which clearly answers the question:
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.
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 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() ?
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
The function call looks like (Syntax):
Some examples to clarify the concept of radix
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.
var a = parseInt("10011", 16);
Here the radix tells the
10011 is a hexadecimal number and hence in integer, it gets converted to
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.
©2020 All rights reserved.