How to Convert Numbers to Words using Indian Numbering in Google Sheets

Advertisements

Hello, Guys welcome back Here learn How to Convert Numbers to Words using Indian Numbering in Google Sheets. Using the lakhs and crores grouping, use a custom Google Sheets function to translate numbers to words in Indian Rupees.

For huge numbers, the Indian numbering and money system frequently use lakhs and crores. A lakh is one hundred thousand, but a crore is ten million. A billion is written as hundred crores, and there are greater denominations such as arab, kharabh, or neel, which are equivalent to 10 trillion.

If you want to write financial amounts in Google Sheets using the Indian numbering system (lakhs, crores), here’s a custom Sheets function INR() that will spell out the numeric value for you. Also Learn How to Handle GET and POST HTTP Requests in Google Apps Script easily.

Advertisements

Convert Numbers to Indian Rupees in Google Sheets

To begin, navigate to your Google Sheet, click the Tools menu, and then pick Script Editor. To save your modifications, copy and paste the function below into the Script Editor and select File > Save.

Switch to Google Sheets, put =INR(123) in any cell, and the value will be instantaneously spelled in English using the lakhs and crores system. Amit Wilson created the function, which was later adapted to work with the V8 Runtime.

/**
 * Convert number to words in Indian Rupees
 *
 * @param {number} input The value to convert.
 * @return The number in lakhs and crores.
 * @customfunction
 */
function INR(input) {
  const rupees = Number(parseInt(input, 10));
  const output = [];

  if (rupees === 0) {
    output.push('zero');
  } else if (rupees === 1) {
    output.push('one');
  } else {
    const crores = Math.floor(rupees / 10000000) % 100;
    if (crores > 0) {
      output.push(`${getHundreds(crores)} crore`);
    }

    const lakhs = Math.floor(rupees / 100000) % 100;
    if (lakhs > 0) {
      output.push(`${getHundreds(lakhs)} lakh`);
    }

    const thousands = Math.floor(rupees / 1000) % 100;
    if (thousands > 0) {
      output.push(`${getHundreds(thousands)} thousand`);
    }

    const hundreds = Math.floor((rupees % 1000) / 100);
    if (hundreds > 0 && hundreds < 10) {
      output.push(`${getOnes(hundreds)} hundred`);
    }

    const tens = rupees % 100;
    if (tens > 0) {
      if (rupees > 100) output.push('and');
      output.push(`${getHundreds(tens)}`);
    }
  }

  return ['Rupees', ...output, 'only']
    .join(' ')
    .split(/\s/)
    .filter((e) => e)
    .map((e) => e.substr(0, 1).toUpperCase() + e.substr(1))
    .join(' ');
}

function getOnes(number) {
  const ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
  return ones[number] || '';
}

function getTeens(number) {
  const teens = [
    'ten',
    'eleven',
    'twelve',
    'thirteen',
    'fourteen',
    'fifteen',
    'sixteen',
    'seventeen',
    'eighteen',
    'nineteen',
  ];
  return teens[number] || '';
}

function getTens(number) {
  const tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
  return tens[number] || '';
}

function getHundreds(num) {
  if (num > 0 && num < 10) {
    return getOnes(num);
  }
  if (num >= 10 && num < 20) {
    return getTeens(num % 10);
  }
  if (num >= 20 && num < 100) {
    return `${getTens(Math.floor(num / 10))} ${getOnes(num % 10)}`;
  }
  return '';
}

The Google Sheets function will only be available in the spreadsheet to which the preceding code has been inserted. If you duplicate the spreadsheet, the function will be duplicated as well.

Advertisements

Leave a Comment