SHARE
TWEET

Untitled

a guest Aug 22nd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2.     HashMap<Integer, String> base = new HashMap<>();
  3.     public Solution() {
  4.         base.put(0, "");
  5.         base.put(1, "One ");
  6.         base.put(2, "Two ");
  7.         base.put(3, "Three ");
  8.         base.put(4, "Four ");
  9.         base.put(5, "Five ");
  10.         base.put(6, "Six ");
  11.         base.put(7, "Seven ");
  12.         base.put(8, "Eight ");
  13.         base.put(9, "Nine ");
  14.         base.put(10, "Ten ");
  15.         base.put(11, "Eleven ");
  16.         base.put(12, "Twelve ");
  17.         base.put(13, "Thirteen ");
  18.         base.put(14, "Fourteen ");
  19.         base.put(15, "Fifteen ");
  20.         base.put(16, "Sixteen ");
  21.         base.put(17, "Seventeen ");
  22.         base.put(18, "Eighteen ");
  23.         base.put(19, "Nineteen ");
  24.        
  25.         base.put(20, "Twenty ");
  26.         base.put(30, "Thirty ");
  27.         base.put(40, "Forty ");
  28.         base.put(50, "Fifty ");
  29.         base.put(60, "Sixty ");
  30.         base.put(70, "Seventy ");
  31.         base.put(80, "Eighty ");
  32.         base.put(90, "Ninety ");
  33.     }
  34.     public String numberToWords(int num) {
  35.         StringBuilder sb = new StringBuilder();
  36.         int billions = num/1_000_000_000;
  37.         if(billions != 0) {
  38.             processUnder1000(billions, sb);
  39.             sb.append("Billion ");
  40.         }
  41.         num %= 1_000_000_000;
  42.         int millions = num/1_000_000;
  43.         if(millions != 0) {
  44.             processUnder1000(millions, sb);
  45.             sb.append("Million ");
  46.         }
  47.         num %= 1_000_000;
  48.         int thousands = num/1000;
  49.         if(thousands != 0) {
  50.             processUnder1000(thousands, sb);
  51.             sb.append("Thousand ");
  52.         }
  53.         num %= 1000;
  54.         processUnder1000(num, sb);
  55.         if(sb.length() == 0) {
  56.             return "Zero";
  57.         } else {
  58.             sb.setLength(sb.length() - 1);
  59.             return sb.toString();
  60.         }
  61.     }
  62.    
  63.     void processUnder1000(int num, StringBuilder sb) {
  64.         if(num < 20) {
  65.             sb.append(base.get(num));
  66.         } else {
  67.             if(num > 99) {
  68.                 sb.append(base.get(num/100)).append("Hundred ");
  69.                 processUnder1000(num%100, sb);
  70.             } else {
  71.                 num%=100;
  72.                 int rem = num%10;
  73.                 num -= rem;
  74.                 sb.append(base.get(num)).append(base.get(rem));                
  75.             }
  76.         }
  77.     }
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top