Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting amount to words

 
nix man
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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);

}

}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nix man:

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


only?
 
shailesh Jammie
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Syed Asad Manzoor
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic