Thursday 27 December 2012

Number To Words In Java - Source Code

The invoices now-a-days carry not only the invoice amount but the amount in words also (Though it is not a requirement, and I personally don't see any usefulness of showing the amount in words).

While converting a number to words, we have to find a solution that is unique to the Indian Numbering System (INS).

The INS is different from the Western Numbering System (WNS) in the sense that after the first grouping that is done on thousand, the rest of the groupings are done on hundred. So, for a amount like 374892, the conversion in INS would read as "Three Lakh (or Lac) Seventy Four Thousand Eight Hundred & Ninety Two"; While this same number would be converted to WNS as "Three Hundred Seventy Four Thousand Eight Hundred & Ninety Two". The words Million, Billion, Trillion, Zillion and so forth are alien to INS, whereas WNS is ignorant of the words Lakh (or Lac), Crore, Paisa.

In one of my applications, I had to write code to convert amount to words, and the result of which is the class "NumberToWords". The typical usage of this class is as under:

String numberInWords = NumberToWords.convertNumberToWords(numberVariable);

Here, numberVariable is of type java.math.BigDecimal and can be declared and initialised like this:

BigDecimal numberVariable =  new BigDecimal("1094.75");

To download the code, please visit this link.


Below is a table that shows numbers on the left and their corresponding conversion to words on the right done by the aforesaid class.


Amount Conversion To Words
1084.72 One Thousand Eighty Four & Seventy Two Paise
0.96 Ninety Six Paise
558972691 Fifty Five Crore Eighty Nine Lakh Seventy Two Thousand Six Hundred Ninety One
10932756847 One Thousand Ninety Three Crore Twenty Seven Lakh Fifty Six Thousand Eight Hundred Forty Seven
0.00 Zero
7 Seven
11.15 Eleven & Fifteen Paise
109 One Hundred Nine
-111.11 Minus One Hundred Eleven & Eleven Paise

Trust this is of use to somebody. And thank you for your time for reading this post. :-))

1 comment: