The Artima Developer Community
Sponsored Link

Java Answers Forum
Number Names

3 replies on 1 page. Most recent reply: Aug 28, 2006 11:13 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Number Names Posted: Aug 23, 2006 5:09 PM
Reply to this message Reply
Advertisement
I did some searching and couldn't find anything on this. Maybe someone here can help.

I'm writing a program to print checks to payees. As far as simply printing the payee name, date, numeric amount, etc. it's pretty straightforward. But a check includes a section where the amount is written out, as well. So, for instance, if the program writes a check for "$53.82", it has to also write out "fifty-three dollars and 82 cents". And not "five-hundred thirty-eight dollars and two cents" or "five dollars and 382 cents".

Is there a method in any of the number related classes that can translate a numeric value into a String? Or some other class I might be overlooking? Or some API/class that someone else may have written that can do this translation? Seems like writing it from scratch is going to be a pretty hefty job.

Thanks in advance for any help anyone might have.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Number Names Posted: Aug 28, 2006 4:50 AM
Reply to this message Reply
I remember that we allready had this discussion once. It was homework of some sort.

First thing to do is to split the number on the decimal separator.

Then split the integer part in groups of 3 digits. (one method would be to repeatly divide by 1000 to get the groups)

It's possible that the first group has only one or two digits.

For the names create String arrays:
String[] names = new String[]{"", "one", "two", "three", "four", "five", "six", "secen", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "sixteen", "eighteen", "nineteen"};
 
//We don't need names for everything up to nineteen
String[] names10 = new String[]{"", "", "twenty", "thirty", "fourty", "fifty", "sixty", "eighty", "ninety"};
 
String[] groupnames = new String[]{"", "thousand", "million", "billion", ...};
 
//Every one of your groups is a integer from 0 to 999.
//This example should give you an idea how to crate the complete number. It may contain some errors, I didn't compile it.
 
int groupValue = ...;
 
int firstDigit = groupValue / 100; //DIV operation cut's off the rest digits.
int secondDigit = (groupValue / 10) % 10;
int thirdDigit = groupValue % 10;
 
String name = "";
if (firstDigit != 0) {
    name += names[firstDigit] + "hundred";
}
if (secondDigit != 0 || thirdDigit != 0) {
    if (firstDigit != 0) {
        name += " and ";
    }
    if (secondDigit < 1) {
        name += names[secondDigit * 10 + thirdDigit];
    } else {
        name += names10[secondDigit] + names[thirdDigit];
    }
}
 

Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Re: Number Names Posted: Aug 28, 2006 11:03 AM
Reply to this message Reply
Thanks for the reply. That should do it.

I'm wondering whether the bank would accept a check if it was written as "53 dollars and 82 cents". Anyone know anything about standards the banks use?

Along related lines, anyone know whether they'll honor a check in which the signature is printed and not actually signed? (Moderators--Sorry, I know it's not a real Java question, but it is somewhat related).

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Number Names Posted: Aug 28, 2006 11:13 PM
Reply to this message Reply
I think they don't accept numbers in the field.

Flat View: This topic has 3 replies on 1 page
Topic: Event Handlers Previous Topic   Next Topic Topic: File Binding

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use