App Script function for converting number to words

function INR(number) {
  // Function to convert a non-negative integer to words
  function numberToWords(n) {
    var ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
    var tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];

    if (n <= 19) {
      return ones[n];
    } else if (n <= 99) {
      return tens[Math.floor(n / 10)] + (n % 10 !== 0 ? " " + ones[n % 10] : "");
    } else {
      return numberToWords(Math.floor(n / 100)) + " hundred" + (n % 100 !== 0 ? " " + numberToWords(n % 100) : "");
    }
  }

  // Function to group numbers by lakhs and crores
  function groupByLakhs(n) {
    var crore = Math.floor(n / 10000000);
    n %= 10000000;
    var lakh = Math.floor(n / 100000);
    n %= 100000;
    var thousand = Math.floor(n / 1000);
    var rest = n % 1000;

    var words = "";
    if (crore > 0) {
      words += numberToWords(crore) + " crore";
    }
    if (lakh > 0) {
      words += (words !== "" ? " " : "") + numberToWords(lakh) + " lakh";
    }
    words += (words !== "" ? " " : "") + numberToWords(thousand) + " thousand";
    words += (words !== "" ? " " : "") + numberToWords(rest);
    return words;
  }

  // Handle negative numbers and non-numeric input
  if (number < 0) {
    return "Negative numbers not supported";
  } else if (isNaN(number)) {
    return "Invalid input";
  }

  // Convert the number to words with lakhs and crores grouping
  return groupByLakhs(number) + " rupees";
}




function numberToWords(number) {
  const ones = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
  const teens = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
  const tens = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
  const suffixes = ['', 'Thousand', 'Lakh', 'Crore'];

  const numToWords = (num) => {
    if (num === 0) return '';

    let words = '';
    for (let i = 0; i < suffixes.length && num > 0; i++) {
      if (num % 1000 !== 0) {
        words = numToWordsHelper(num % 1000) + suffixes[i] + ' ' + words;
      }
      num = Math.floor(num / 1000);
    }
    return words.trim();
  };

  const numToWordsHelper = (num) => {
    let words = '';

    if (num >= 100) {
      words += ones[Math.floor(num / 100)] + ' Hundred ';
      num %= 100;
    }

    if (num >= 10 && num <= 19) {
      words += teens[num - 10] + ' ';
      num = 0; // Reset num to 0 as it's already handled in teens array
    } else if (num >= 20) {
      words += tens[Math.floor(num / 10)] + ' ';
      num %= 10;
    }

    if (num > 0) {
      words += ones[num] + ' ';
    }

    return words;
  };

  if (isNaN(number) || number < 0 || number >= 1e12) {
    return 'quotealid number';
  }

  const rupees = Math.floor(number);
  const paise = Math.round((number - rupees) * 100);

  let words = numToWords(rupees);
  words = words === '' ? 'Zero' : words;

  let result = 'Rupees ' + words + ' Only';

  if (paise > 0) {
    words = numToWords(paise);
    result += ' and ' + words + ' Paise Only';
  }

  return result;
}

Leave a Reply