Open In App

JavaScript Program for Decimal to any base conversion

Last Updated : 20 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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
  • Using the loop (do-while)
  • Using the Array
  • Using the Recursion

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


Output

The 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}`);


Output

The 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}`);


Output

The 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}`);


Output

The Decimal 1100 in base 2 is: 10001001100


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads