Approach 3 Using parseInt and toString

  • Parse the input strings num1 and num2 to regular numbers using parseInt().
  • Check for division by zero by verifying if number2 is equal to zero, and throw an error if it is.
  • Perform the division using the / operator between number1 and number2, and store the result.
  • Convert the result to a string, split it at the decimal point, and keep only the integer part for the final result. Return this integer part as a string.

Syntax:

const number1 = parseInt(num1, 10);
const number2 = parseInt(num2, 10);

Example: Below is the implementation of the above approach.

Javascript
function divideLargeNumbers(num1, num2) {

    // Parse the input strings to numbers
    const number1 = parseInt(num1, 10);
    const number2 = parseInt(num2, 10);

    // Check if num2 is zero (division by zero)
    if (number2 === 0) {
        throw new Error("Division by zero is not allowed.");
    }

    // Perform the division
    const result = number1 / number2;

    // Convert the result back to a string
    return result.toString().split('.')[0];
}

// Example usage:
const num1 = "1322145464651";
const num2 = "125";
const result = divideLargeNumbers(num1, num2);
console.log(result);

Output
10577163717

JavaScript Program to Divide Large Number Represented as String

In this article, we will explore dividing large numbers represented as strings in JavaScript. Dividing a large number represented as a string in JavaScript is a common computational task that involves dividing a numerical line by another numerical value.

Table of Content

  • Approach 1: Brute Force
  • Appraoch 2: Using BigInt() in JavaScript
  • Approach 3: Using parseInt and toString:

Similar Reads

Approach 1: Brute Force

We will use basic maths, as the dividend and result can be huge we store them in string. We first take digits which are divisible by a number. After this take each digit and store the result in a string....

Appraoch 2: Using BigInt() in JavaScript

Convert the input strings num1 and num2 to BigInt.Check for division by zero (if bigint2 is zero), and throw an error if necessary.Perform the division using the / operator between bigint1 and bigint2, and store the result.Return the result of the division as a string....

Approach 3: Using parseInt and toString:

Parse the input strings num1 and num2 to regular numbers using parseInt().Check for division by zero by verifying if number2 is equal to zero, and throw an error if it is.Perform the division using the / operator between number1 and number2, and store the result.Convert the result to a string, split it at the decimal point, and keep only the integer part for the final result. Return this integer part as a string....

Contact Us