Transform Numbers to Words in Lakh/Crore System

Transform numbers to words in lakh / crore system

Update: Looks like this is more useful than I thought. I've just published this on npm. https://www.npmjs.com/package/num-words


Here's a shorter code. with one RegEx and no loops. converts as you wanted, in south asian numbering system

var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
function inWords (num) { if ((num = num.toString()).length > 9) return 'overflow'; n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/); if (!n) return; var str = ''; str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : ''; str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : ''; str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : ''; str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : ''; str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : ''; return str;}
document.getElementById('number').onkeyup = function () { document.getElementById('words').innerHTML = inWords(document.getElementById('number').value);};
<span id="words"></span><input id="number" type="text" />

Is there an easy way to convert a number to Indian words format with lakhs and crores?

While I can't give you the code itself, here's the system

  • 1 - One
  • 10 - Ten
  • 1000 - Thousand
  • 10000 - Ten Thousand
  • 100000 - Lakh
  • 1000000 - Ten Lakh
  • 10000000 - Crore
  • 100000000 - Ten Crore
  • 1000000000 - Arab
  • 10000000000 - Ten Arab
  • 100000000000 - Kharab
  • 1000000000000 - Ten Kharab
  • 10000000000000 - 1 Neel
  • 100000000000000 - 10 Neel
  • 1000000000000000 - 1 Padm

I had written the routines for an accounting package I made, so writing it is not particularly hard.

How to format a number to Lac or Crores if it has minus sign before it

You can use Math.abs().

This function returns the absolute value of a number

function numDifferentiation(value) {  var val = Math.abs(value)  if (val >= 10000000) {    val = (val / 10000000).toFixed(2) + ' Cr';  } else if (val >= 100000) {    val = (val / 100000).toFixed(2) + ' Lac';  }  return val;}console.log(numDifferentiation(-50000000))

function to convert amount into Indian words (Crores, Lakhs, Thousands ...) in R language

Seems that I have figured it out myself. Interested users may test and report bugs, if any.

# function to convert number to words in Indian counting system
# https://en.wikipedia.org/wiki/Indian_numbering_system#Names_of_numbers

# number always rounded; multiple numbers also accepted as vector
# currently handles numbers upto 15 digits but can be easily extended

# Credit: A big THANKS to Mr. John Fox.
# His function to convert number to English words can be found at:
# http://tolstoy.newcastle.edu.au/R/help/05/04/2715.html

SpellIndianNumber <- function(x){

helper <- function(x){

digits <- rev(strsplit(as.character(x), "")[[1]])
nDigits <- length(digits)

# function meant to handle numbers upto 15 digits only
if(nDigits > 15) stop("Number is too large!")

# single digit cases: 1 to 9
if (nDigits == 1) as.vector(ones[digits])

# two digits cases: 10 to 99
else if (nDigits == 2)
if (x <= 19) as.vector(teens[digits[1]]) # for 10 to 19
else trim(paste(tens[digits[2]], # for 20 to 99
Recall(as.numeric(digits[1]))))

# three digits cases: 100 to 999
else if (nDigits == 3) trim(paste(ones[digits[3]], "Hundred",
Recall(makeNumber(digits[2:1]))))

# four & five digits cases: handling thousands
else if (nDigits <= 5){
thousands <- x %/% 1e3
remainder <- x %% 1e3
trim(paste(Recall(thousands), "Thousand", Recall(remainder)))
}

# six & seven digits cases: handling lakhs
else if (nDigits <= 7){
lakhs <- x %/% 1e5
remainder <- x %% 1e5
trim(paste(Recall(lakhs), "Lakh", Recall(remainder)))
}

# eight & nine digits cases: handling crores
else if (nDigits <= 9){
crores <- x %/% 1e7
remainder <- x %% 1e7
trim(paste(Recall(crores), "Crore", Recall(remainder)))
}

# ten & eleven digits cases: handling arabs
else if (nDigits <= 11){
arabs <- x %/% 1e9
remainder <- x %% 1e9
trim(paste(Recall(arabs), "Arab", Recall(remainder)))
}

# twelve & thirteen digits cases: handling kharabs
else if (nDigits <= 13){
kharabs <- x %/% 1e11
remainder <- x %% 1e11
trim(paste(Recall(kharabs), "Kharab", Recall(remainder)))
}

# fourteen & fifteen digits cases: handling neels
else if (nDigits <= 15){
neels <- x %/% 1e13
remainder <- x %% 1e13
trim(paste(Recall(neels), "Neel", Recall(remainder)))
}
}

trim <- function(text){
gsub("^\ ", "", gsub("\ *$", "", text))
}

makeNumber <- function(...) as.numeric(paste(..., collapse=""))

opts <- options(scipen=100)
on.exit(options(opts))

ones <- c("", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine")
names(ones) <- 0:9

teens <- c("Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", " Seventeen", "Eighteen", "Nineteen")
names(teens) <- 0:9

tens <- c("Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety")
names(tens) <- 2:9

x <- round(x)

if (length(x) > 1) return(sapply(x, helper))
helper(x)
}

# Examples:

# > SpellIndianNumber(83720834)
# [1] "Eight Crore Thirty Seven Lakh Twenty Thousand Eight Hundred Thirty Four"

# > SpellIndianNumber(1283720834)
# [1] "One Arab Twenty Eight Crore Thirty Seven Lakh Twenty Thousand Eight Hundred Thirty Four"

# > SpellIndianNumber(653234532342345)
# [1] "Sixty Five Neel Thirty Two Kharab Thirty Four Arab Fifty Three Crore Twenty Three Lakh Forty Two Thousand Three Hundred Forty Five"

# > SpellIndianNumber(c(5,10,87))
# [1] "Five" "Ten" "Eighty Seven"

# > SpellIndianNumber(11:15)
# [1] "Eleven" "Twelve" "Thirteen" "Fourteen" "Fifteen"

# Number Zero will appear as a zero length string
# > SpellIndianNumber(0)
# [1] ""

# > SpellIndianNumber(c(12,0,87))
# [1] "Twelve" "" "Eighty Seven"

JavaScript numbers to Words

JavaScript is parsing the group of 3 numbers as an octal number when there's a leading zero digit. When the group of three digits is all zeros, the result is the same whether the base is octal or decimal.

But when you give JavaScript '009' (or '008'), that's an invalid octal number, so you get zero back.

If you had gone through the whole set of numbers from 190,000,001 to 190,000,010 you'd hav seen JavaScript skip '...,008' and '...,009' but emit 'eight' for '...,010'. That's the 'Eureka!' moment.

Change:

for (j = 0; j < finlOutPut.length; j++) {
finlOutPut[j] = triConvert(parseInt(finlOutPut[j]));
}

to

for (j = 0; j < finlOutPut.length; j++) {
finlOutPut[j] = triConvert(parseInt(finlOutPut[j],10));
}

Code also kept on adding commas after every non-zero group, so I played with it and found the right spot to add the comma.

Old:

for (b = finlOutPut.length - 1; b >= 0; b--) {
if (finlOutPut[b] != "dontAddBigSufix") {
finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr] + ' , ';
bigScalCntr++;
}
else {
//replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
finlOutPut[b] = ' ';
bigScalCntr++; //advance the counter
}
}

//convert The output Arry to , more printable string
for(n = 0; n<finlOutPut.length; n++){
output +=finlOutPut[n];
}

New:

for (b = finlOutPut.length - 1; b >= 0; b--) {
if (finlOutPut[b] != "dontAddBigSufix") {
finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr]; // <<<
bigScalCntr++;
}
else {
//replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
finlOutPut[b] = ' ';
bigScalCntr++; //advance the counter
}
}

//convert The output Arry to , more printable string
var nonzero = false; // <<<
for(n = 0; n<finlOutPut.length; n++){
if (finlOutPut[n] != ' ') { // <<<
if (nonzero) output += ' , '; // <<<
nonzero = true; // <<<
} // <<<
output +=finlOutPut[n];
}


Related Topics



Leave a reply



Submit