File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes converting amount to words Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "converting amount to words" Watch "converting amount to words" New topic

converting amount to words

nix man

Joined: Feb 22, 2005
Posts: 1
Hi all,

I need to help. I am working on a billing project using jsp&mysql, Where the total amount should be calculated and displayed Numeric as well as Text.
For ex:

Rs. 2500/-
Rupees Two thousand Five Hundred only.

Can somebody help me to this text conversion.

Thanks in advance

nix man
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

This is more a basic Java issue than a JSP one.

Another place to take a peek is the "cattle drive" forum.
I believe one of the projects that students do involves just this, describing numbers with text. I'm sure that, if your read through the threads in that forum, you will get some good hints.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
An excellent problem! For fun (I mean it, fun!) see if you can make a program that works for 0 through 9. Then for 10 through 19. Then for 20 through 99. Then for 100 through 999. You can actually do it without erasing too much code from one run to make the next one work.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
Hope this helps.

public class Demo {

private static final String[] ones =
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen" };

private static final String[] tens =
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety" };

// so quintillions is as big as it gets. The
// program would automatically handle larger
// numbers if this array were extended.
private static final String[] groups =
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion" };

private String string = new String();

public String getString() {
return string;

public String EnglishNumber(long enteredNo) {

// Go through the number one group at a time.
//System.out.println("groups.length= " + groups.length);
for (int i = groups.length - 1; i >= 0; i--) {

// Is the number as big as this group?

long cutoff = (long) Math.pow((double) 10, (double) (i * 3));

if (enteredNo >= cutoff) {
int thisPart = (int) (enteredNo / cutoff);

// Use the ones[] array for both the
// hundreds and the ones digit. Note
// that tens[] starts at "twenty".

if (thisPart >= 100) {
string += ones[(thisPart / 100) - 1] + " hundred";

thisPart = thisPart % 100;
if (thisPart >= 20) {
string += tens[(thisPart / 10) - 2];
thisPart = thisPart % 10;
if (thisPart >= 1) {
string += ones[thisPart - 1];

string += groups[i];

enteredNo = enteredNo % cutoff;//to check for big numbers which are greater than or equal to 1 million
//System.out.println("enteredNo is : " + enteredNo);

if (string.length() == 0) {
string = "zero";
} else {
// remove initial space
string = string.substring(1);
return string;

public static void main(String args[]) {

Demo ntw = new Demo();
String Englishword = null;
Englishword = ntw.EnglishNumber(1000000);
System.out.println("The entered number in words is : " + Englishword);



- Christian
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Okay, now internationalize your appliction I was just thinking about this recently because I was helping my seven year-old with her French homework and she's learning her numbers: 92 = "quatre-vingt-douze" (four twenties and twelve).

There is no emoticon for what I am feeling!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by nix man:

Rs. 2500/-
Rupees Two thousand Five Hundred only.

shailesh Jammie

Joined: Nov 14, 2005
Posts: 11
Hi Ashwin,
good effort.
but i think it will not work properly more or equal to 6 digit.
i know we can do this by adding more into String array.
but the question is there can be general way to convert any number of any length.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
There's an important mistake in the big long example solution. It uses Math.pow(). The problem being solved is entirely an integer arithmetic problem and no floating-point operations are necessary. They could give accuracy/rounding problems and are also unnecessarily slow.

To do powers of ten in integer arithmetic, a usually-good-enough solution isuse a simple "for" loop. If you need to optimise performance (and you very rarely actually do need to do so), store pre-calculated values in an array.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Syed Asad Manzoor

Joined: May 27, 2009
Posts: 1
Hi though i am replying to a very old thread, but hope this will be helpful. The code does not use any floating point calculation and Math.pow function.
I agree. Here's the link:
subject: converting amount to words
It's not a secret anymore!