JavaScript Program for Decimal to any base conversion
Last Updated :
04 Jun, 2024
In this JavaScript article, we will see how we can do decimal to any base conversion in JavaScript. The base can not be less than 2 and can not exceed 36, So we always have to find out the base of a decimal that lies in between this range, which is '2=< base <=36'.
Example:
Input: number = "1100", base = 2
Output: 10001001100
There are different types of methods to perform decimal to any base conversion in JavaScript:
Using the 'toString()' method in JavaScript
The toString method takes the input as the decimal number and also the base value, converts this into the equivalent base, and prints using the log() function.
Example: In this example, we are performing decimal to any base conversion using the 'toString' method
JavaScript
function decToBase(inputNumber, inputBase) {
if (inputBase < 2 || inputBase > 36) {
return "Not Valid Base";
}
return inputNumber.toString(inputBase);
}
let inputNo = 1100;
let inputBase = 2;
let output = decToBase(inputNo, inputBase);
console.log(
`The Decimal ${inputNo} in base ${inputBase} is: ${output}`);
OutputThe Decimal 1100 in base 2 is: 10001001100
Using the loop (do-while)
In this approach we are using the do while loop where we iterate and convert the decimal input number to the equivalent base by repeatedly calculating the remainders and then creating a result in the reverse order. We check the base is valid or not also.
Example: In this example we are performing decimal to any base conversion using the do-while loop.
JavaScript
function decToBase(inputNumber, inputBase) {
if (inputBase < 2 || inputBase > 36) {
return "Not Valid Base";
}
let tempOutput = "";
let checkNum = false;
if (inputNumber < 0) {
checkNum = true;
inputNumber = Math.abs(inputNumber);
}
do {
let rem = inputNumber % inputBase;
tempOutput = rem + tempOutput;
inputNumber = Math.floor(inputNumber / inputBase);
} while (inputNumber > 0);
if (checkNum) {
tempOutput = "-" + tempOutput;
}
return tempOutput || "0";
}
let inputNo = 1100;
let inputBase = 2;
let output = decToBase(inputNo, inputBase);
console.log
(`The Decimal ${inputNo} in base ${inputBase} is: ${output}`);
OutputThe Decimal 1100 in base 2 is: 10001001100
Using the Array
In this approach, we are using the array to store the digits of the result in the reverse order while we divide the decimal number by the input base value. We are adding each digit to the result array ad then joining the array elements.
Example: In this example, we are performing decimal to any base conversion using array.
JavaScript
function decToBase(inputNumber, inputBase) {
if (inputBase < 2 || inputBase > 36) {
return "Not Valid Base";
}
let digits =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let resArray = [];
if (inputNumber === 0) {
return "0";
} else if (inputNumber < 0) {
return "-" + decToBase(-inputNumber, inputBase);
}
while (inputNumber > 0) {
let rem = inputNumber % inputBase;
resArray.unshift(digits[rem]);
inputNumber = Math.floor(inputNumber / inputBase);
}
return resArray.join('');
}
let inputNo = 1100;
let inputBase = 2;
let output = decToBase(inputNo, inputBase);
console.log(
`The Decimal ${inputNo} in base ${inputBase} is: ${output}`);
OutputThe Decimal 1100 in base 2 is: 10001001100
Using the Recursion
In this approach, we are using the recursive function that converts a input decimal number to its equivalent base. This apporach also properly validates the base range and gives correct output.
Example: In this example, we are performing decimal to any base conversion using Recursion
JavaScript
function decToBase(inputNumber, inputBase) {
if (inputBase < 2 || inputBase > 36) {
return "Not Vaild Base";
}
if (inputNumber === 0) {
return '0';
}
let rem = inputNumber % inputBase;
let quo = Math.floor(inputNumber / inputBase);
if (quo === 0) {
return rem.toString();
} else {
return decToBase(quo, inputBase) + rem.toString();
}
}
let inputNo = 1100;
let inputBase = 2;
let output = decToBase(inputNo, inputBase);
console.log
(`The Decimal ${inputNo} in base ${inputBase} is: ${output}`);
OutputThe Decimal 1100 in base 2 is: 10001001100
Using Bitwise Operations
In this approach, we utilize bitwise operations to convert a decimal number to its equivalent binary representation. This method is specifically efficient for base 2 conversions. Bitwise operations directly manipulate the bits of a number, making it a fast and efficient method for binary conversion.
Example: In this example, we are performing decimal to binary conversion using bitwise operations.
JavaScript
function decToBaseBitwise(inputNumber, inputBase) {
if (inputBase !== 2) {
return "This method only supports base 2";
}
if (inputNumber === 0) {
return "0";
}
let result = "";
let checkNum = false;
if (inputNumber < 0) {
checkNum = true;
inputNumber = Math.abs(inputNumber);
}
while (inputNumber > 0) {
result = (inputNumber & 1) + result;
inputNumber = inputNumber >> 1;
}
if (checkNum) {
result = "-" + result;
}
return result;
}
let inputNo = 1100;
let inputBase = 2;
let output = decToBaseBitwise(inputNo, inputBase);
console.log(`The Decimal ${inputNo} in base ${inputBase} is: ${output}`);
OutputThe Decimal 1100 in base 2 is: 10001001100
Similar Reads
JavaScript Program to Convert Decimal to Binary
In this article, we are going to learn the conversion of numeric values from decimal to binary. Binary is a number system with 2 digits (0 and 1) representing all numeric values. Given a number N which is in decimal representation. our task is to convert the decimal representation of the number to i
5 min read
JavaScript Program for Double to String Conversion
Converting a double (floating-point) number to a string in JavaScript involves transforming the numeric value into its corresponding string representation. This process is essential when dealing with numeric data that needs to be displayed or manipulated as strings, such as in user interfaces or dat
1 min read
JavaScript Program to Convert Decimal to Binary Using Recursion
JavaScript allows us to convert decimal numbers to their binary equivalents using recursion, offering two distinct approaches. Through recursive division and concatenation, the first method constructs the binary representation directly. The second method leverages a string and recursion, providing a
2 min read
JavaScript Program to Convert a Number to Binary
We are going to learn the conversion of a number to binary by using JavaScript, Convert a number to binary in JavaScript refers to the process of converting a decimal number into its binary representation, which uses only the digits 0 and 1 to represent the value Converting a number to binary in Jav
3 min read
C Program For Octal to Decimal Conversion
The number system is one of the ways to represent numbers. Every number system has its own base or radix. For example, Binary, Octal, Decimal, and Hexadecimal Number systems are some of the number systems and are also used in microprocessor programming. These numbers are easy to convert from one sys
3 min read
Binary to Decimal Conversion in Java
You are given a binary number as input and your task is to convert that number to its decimal equivalent through a Java program. Examples : Input : 1100Output : 12Input : 1111Output : 15Methods for Binary to Decimal ConversionsThere are certain methods used for Binary to Decimal Conversions mentione
5 min read
Java Program to Convert Binary to Hexadecimal
The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15.
6 min read
How to convert decimal to octal in JavaScript ?
In this article, we are given a number and the task is to convert the number from decimal to octal. This can be done by using number.toString(8) method. It takes the parameter which is the base of the converted string. In this case, the base will be 8. Syntax: number.toString(8) The below examples s
2 min read
Convert a String to an Integer in JavaScript
These are the following ways to convert string to an integer in JavaScript: 1. Using Number() MethodThe number() method converts a string into an integer number. It works similarly to the unary plus operator. [GFGTABS] JavaScript let s = "100.45"; console.log(typeof Number(s)); [/GFGTABS]O
2 min read
How to convert string of any base to an integer in JavaScript ?
Given a string containing an integer value and along with that user passes a base value. We need to convert that string of any base value to an integer in JavaScript. String Integer "1002" 1002 For performing the above-illustrated task, we would be using a method (or a function) provided by JavaScri
3 min read