Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform numbers to words in lakh / crore system

I'm writing some code that converts a given number into words, here's what I have got after googling. But I think it's a bit too long for such a simple task. Two Regular Expressions and two for loops, I want something simpler.

I am trying to achieve this in as few lines of code as possible. here's what I've come up with so far:

Any suggestions?

var th = ['','thousand','million', 'billion','trillion']; var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine'];  var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];  var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];   function toWords(s) {     s = s.toString();     s = s.replace(/[\, ]/g,'');     if (s != parseFloat(s)) return 'not a number';     var x = s.indexOf('.');     if (x == -1)         x = s.length;     if (x > 15)         return 'too big';     var n = s.split('');      var str = '';     var sk = 0;     for (var i=0;   i < x;  i++) {         if ((x-i)%3==2) {              if (n[i] == '1') {                 str += tn[Number(n[i+1])] + ' ';                 i++;                 sk=1;             } else if (n[i]!=0) {                 str += tw[n[i]-2] + ' ';                 sk=1;             }         } else if (n[i]!=0) { // 0235             str += dg[n[i]] +' ';             if ((x-i)%3==0) str += 'hundred ';             sk=1;         }         if ((x-i)%3==1) {             if (sk)                 str += th[(x-i-1)/3] + ' ';             sk=0;         }     }          if (x != s.length) {         var y = s.length;         str += 'point ';         for (var i=x+1; i<y; i++)             str += dg[n[i]] +' ';     }     return str.replace(/\s+/g,' '); } 

Also, the above code converts to the English numbering system like Million/Billion, I need the South Asian numbering system, like in Lakhs and Crores.

like image 284
Jeo Avatar asked Feb 08 '13 06:02

Jeo


People also ask

How do you convert numbers into words?

Type the formula =SpellNumber(A1) into the cell where you want to display a written number, where A1 is the cell containing the number you want to convert. You can also manually type the value like =SpellNumber(22.50).

How do you convert numbers to crores?

One crore is equal to 10 million. So, to convert any number from crores to million, you need to multiply the number by 10 and it will become millions. If your number is 5 crores, you multiply 5 by 10 to get 50 million. The same formula can be altered to convert a number from million to crores.

How do you write an INR in Word?

For example ₹ 10, ₹ 20, 50p, 70p. When rupee and paise are written together in figures, it is separated with a (.). Paise is written always as a 2-digit number, for example ₹ 21 and 5 p is written as ₹ 21.05. 25 paise is written as ₹ 0.25.


1 Answers

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" />

The only limitation is, you can convert maximum of 9 digits, which I think is more than sufficient in most cases..

like image 193
Salman Avatar answered Sep 24 '22 07:09

Salman