|
Re: Number Names
|
Posted: Aug 28, 2006 4:50 AM
|
|
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];
}
}
|
|