Skip to content

Instantly share code, notes, and snippets.

@vrkansagara
Created March 15, 2018 19:36
Show Gist options
  • Select an option

  • Save vrkansagara/9bdf6f3bc93fc97dfa4503aa1b58e98a to your computer and use it in GitHub Desktop.

Select an option

Save vrkansagara/9bdf6f3bc93fc97dfa4503aa1b58e98a to your computer and use it in GitHub Desktop.
Number to Word in INR string.
function INR(input) {
var a, b, c, d, e, output, outputA, outputB, outputC, outputD, outputE;
var ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
if (input === 0) { // Zero
output = "Rupees zero";
} else if (input == 1) { // One
output = "Rupee one only";
} else { // More than one
// Tens
a = input % 100;
outputA = oneToHundred_(a);
// Hundreds
b = Math.floor((input % 1000) / 100);
if (b > 0 && b < 10) {
outputB = ones[b];
}
// Thousands
c = (Math.floor(input / 1000)) % 100;
outputC = oneToHundred_(c);
// Lakh
d = (Math.floor(input / 100000)) % 100;
outputD = oneToHundred_(d);
// Crore
e = (Math.floor(input / 10000000)) % 100;
outputE = oneToHundred_(e);
// Make string
output = "Rupees";
if (e > 0) {
output = output + " " + outputE + " crore";
}
if (d > 0) {
output = output + " " + outputD + " lakh";
}
if (c > 0) {
output = output + " " + outputC + " thousand";
}
if (b > 0) {
output = output + " " + outputB + " hundred";
}
if (input > 100 && a > 0) {
output = output + " and";
}
if (a > 0) {
output = output + " " + outputA;
}
output = output + " only";
}
return output;
}
function oneToHundred_(num) {
var outNum;
var ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
var teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
var tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
if (num > 0 && num < 10) { // 1 to 9
outNum = ones[num]; // ones
} else if (num > 9 && num < 20) { // 10 to 19
outNum = teens[(num % 10)]; // teens
} else if (num > 19 && num < 100) { // 20 to 100
outNum = tens[Math.floor(num / 10)]; // tens
if (num % 10 > 0) {
outNum = outNum + " " + ones[num % 10]; // tens + ones
}
}
return outNum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment