class englishnumber { 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 englishnumber ( long n ) { // go through the number one group at a time. 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 ( n >= cutoff ) { int thispart = (int)(n / 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] + " hundred"; thispart = thispart % 100; }

if (thispart >= 20) { string += tens[(thispart/10)-1]; thispart = thispart % 10; } if (thispart >= 1) { string += ones[thispart]; } string += groups[i]; n = n % cutoff; } } if (string.length() == 0) { string = "zero"; } else { // remove initial space string = string.substring(1); } } }

Sign up to vote on this title
UsefulNot useful