Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public 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",
- " firty", " sixty", " seventy", " eighty", " ninety"};
- private static final String[] groups = {"", " thousand", " million",
- " billion", " trillion", " quadrillion", " quintillion"};
- private String string = "";
- public String getString() {
- return string;
- }
- @Override
- public String toString() {
- return getString();
- }
- public EnglishNumber(long n) {
- for (int i = groups.length - 1; i >= 0; i--) {
- long cutoff = (long )Math.pow((double )10, (double )(i * 3));
- if (n >= cutoff) {
- int thisPart = (int )(n / cutoff);
- if (thisPart >= 100) {
- string += ones[thisPart / 100] + " hundred";
- thisPart %= 100;
- }
- if (thisPart >= 20) {
- string += tens[thisPart / 10];
- thisPart %= 10;
- }
- if (thisPart >= 1) {
- string += ones[thisPart];
- }
- string += groups[i];
- n %= cutoff;
- }
- }
- if (string.length() == 0) {
- string = "zero";
- }
- else {
- string = string.substring(1);
- }
- }
- public static void main(String[] args) {
- for (int i = 0; i < 31; i++) {
- System.out.println(new EnglishNumber(i));
- }
- System.out.println(new EnglishNumber(100));
- System.out.println(new EnglishNumber(101));
- System.out.println(new EnglishNumber(1000));
- System.out.println(new EnglishNumber(1001));
- System.out.println(new EnglishNumber(10000));
- System.out.println(new EnglishNumber(10001));
- System.out.println(new EnglishNumber(100000));
- System.out.println(new EnglishNumber(100001));
- System.out.println(new EnglishNumber(1000000));
- System.out.println(new EnglishNumber(1000001));
- System.out.println(new EnglishNumber(10000000));
- System.out.println(new EnglishNumber(10000001));
- System.out.println(new EnglishNumber(100000000));
- System.out.println(new EnglishNumber(100000001));
- System.out.println(new EnglishNumber(1000000000));
- System.out.println(new EnglishNumber(1000000001));
- System.out.println(new EnglishNumber(10000000000L));
- System.out.println(new EnglishNumber(10000000001L));
- System.out.println(new EnglishNumber(100000000000L));
- System.out.println(new EnglishNumber(100000000001L));
- System.out.println(new EnglishNumber(1000000000000L));
- System.out.println(new EnglishNumber(1000000000001L));
- System.out.println(new EnglishNumber(10000000000000L));
- System.out.println(new EnglishNumber(10000000000001L));
- System.out.println(new EnglishNumber(100000000000000L));
- System.out.println(new EnglishNumber(100000000000001L));
- System.out.println(new EnglishNumber(1000000000000000L));
- System.out.println(new EnglishNumber(1000000000000001L));
- System.out.println(new EnglishNumber(10000000000000000L));
- System.out.println(new EnglishNumber(10000000000000001L));
- System.out.println(new EnglishNumber(100000000000000000L));
- System.out.println(new EnglishNumber(100000000000000001L));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement