Advertisement
cd62131

EnglishNumber

Jun 30th, 2014
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.36 KB | None | 0 0
  1. public class EnglishNumber {
  2.   private static final String[] ones = {"", " one", " two", " three", " four",
  3.     " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve",
  4.     " thirteen", " fourteen", " fifteen", " sixteen", " seventeen",
  5.     " eighteen", " nineteen"};
  6.   private static final String[] tens = {"", "", " twenty", " thirty", " forty",
  7.     " firty", " sixty", " seventy", " eighty", " ninety"};
  8.   private static final String[] groups = {"", " thousand", " million",
  9.     " billion", " trillion", " quadrillion", " quintillion"};
  10.   private String string = "";
  11.  
  12.   public String getString() {
  13.     return string;
  14.   }
  15.  
  16.   @Override
  17.   public String toString() {
  18.     return getString();
  19.   }
  20.  
  21.   public EnglishNumber(long n) {
  22.     for (int i = groups.length - 1; i >= 0; i--) {
  23.       long cutoff = (long )Math.pow((double )10, (double )(i * 3));
  24.       if (n >= cutoff) {
  25.         int thisPart = (int )(n / cutoff);
  26.         if (thisPart >= 100) {
  27.           string += ones[thisPart / 100] + " hundred";
  28.           thisPart %= 100;
  29.         }
  30.         if (thisPart >= 20) {
  31.           string += tens[thisPart / 10];
  32.           thisPart %= 10;
  33.         }
  34.         if (thisPart >= 1) {
  35.           string += ones[thisPart];
  36.         }
  37.         string += groups[i];
  38.         n %= cutoff;
  39.       }
  40.     }
  41.     if (string.length() == 0) {
  42.       string = "zero";
  43.     }
  44.     else {
  45.       string = string.substring(1);
  46.     }
  47.   }
  48.  
  49.   public static void main(String[] args) {
  50.     for (int i = 0; i < 31; i++) {
  51.       System.out.println(new EnglishNumber(i));
  52.     }
  53.     System.out.println(new EnglishNumber(100));
  54.     System.out.println(new EnglishNumber(101));
  55.     System.out.println(new EnglishNumber(1000));
  56.     System.out.println(new EnglishNumber(1001));
  57.     System.out.println(new EnglishNumber(10000));
  58.     System.out.println(new EnglishNumber(10001));
  59.     System.out.println(new EnglishNumber(100000));
  60.     System.out.println(new EnglishNumber(100001));
  61.     System.out.println(new EnglishNumber(1000000));
  62.     System.out.println(new EnglishNumber(1000001));
  63.     System.out.println(new EnglishNumber(10000000));
  64.     System.out.println(new EnglishNumber(10000001));
  65.     System.out.println(new EnglishNumber(100000000));
  66.     System.out.println(new EnglishNumber(100000001));
  67.     System.out.println(new EnglishNumber(1000000000));
  68.     System.out.println(new EnglishNumber(1000000001));
  69.     System.out.println(new EnglishNumber(10000000000L));
  70.     System.out.println(new EnglishNumber(10000000001L));
  71.     System.out.println(new EnglishNumber(100000000000L));
  72.     System.out.println(new EnglishNumber(100000000001L));
  73.     System.out.println(new EnglishNumber(1000000000000L));
  74.     System.out.println(new EnglishNumber(1000000000001L));
  75.     System.out.println(new EnglishNumber(10000000000000L));
  76.     System.out.println(new EnglishNumber(10000000000001L));
  77.     System.out.println(new EnglishNumber(100000000000000L));
  78.     System.out.println(new EnglishNumber(100000000000001L));
  79.     System.out.println(new EnglishNumber(1000000000000000L));
  80.     System.out.println(new EnglishNumber(1000000000000001L));
  81.     System.out.println(new EnglishNumber(10000000000000000L));
  82.     System.out.println(new EnglishNumber(10000000000000001L));
  83.     System.out.println(new EnglishNumber(100000000000000000L));
  84.     System.out.println(new EnglishNumber(100000000000000001L));
  85.   }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement