Skip to content

Instantly share code, notes, and snippets.

@faeem81
Created March 11, 2021 08:04
Show Gist options
  • Save faeem81/eda930de88f30a80a8405c1f86ce2205 to your computer and use it in GitHub Desktop.
Save faeem81/eda930de88f30a80a8405c1f86ce2205 to your computer and use it in GitHub Desktop.
SpellNumber Indian Rupees - Google Sheet
/**
* msofficegeek.com Spell Number Indian Rupees
* @customfunction
*/
function SpellNumberINR(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 "";
}
@ajtazer
Copy link

ajtazer commented Jul 11, 2023

Revision
This One Works

@ajtazer of @Fat-Earth

function SpellNumberINR(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 "";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment