SHARE
TWEET

Untitled

a guest Sep 17th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class NumberToWord  
  2. {
  3.     private static final String[] specialNames = {
  4.         "",
  5.         " thousand",
  6.         " million",
  7.         " billion",
  8.         " trillion",
  9.         " quadrillion",
  10.         " quintillion"
  11.     };
  12.    
  13.     private static final String[] tensNames = {
  14.         "",
  15.         " ten",
  16.         " twenty",
  17.         " thirty",
  18.         " forty",
  19.         " fifty",
  20.         " sixty",
  21.         " seventy",
  22.         " eighty",
  23.         " ninety"
  24.     };
  25.    
  26.     private static final String[] numNames = {
  27.         "",
  28.         " one",
  29.         " two",
  30.         " three",
  31.         " four",
  32.         " five",
  33.         " six",
  34.         " seven",
  35.         " eight",
  36.         " nine",
  37.         " ten",
  38.         " eleven",
  39.         " twelve",
  40.         " thirteen",
  41.         " fourteen",
  42.         " fifteen",
  43.         " sixteen",
  44.         " seventeen",
  45.         " eighteen",
  46.         " nineteen"
  47.     };
  48.    
  49.     private String convertLessThanOneThousand(int number) {
  50.         String current;
  51.        
  52.         if (number % 100 < 20){
  53.             current = numNames[number % 100];
  54.             number /= 100;
  55.         }
  56.         else {
  57.             current = numNames[number % 10];
  58.             number /= 10;
  59.            
  60.             current = tensNames[number % 10] + current;
  61.             number /= 10;
  62.         }
  63.         if (number == 0) return current;
  64.         return numNames[number] + " hundred" + current;
  65.     }
  66.    
  67.     public String convert(int number) {
  68.  
  69.         if (number == 0) { return "zero"; }
  70.        
  71.         String prefix = "";
  72.        
  73.         if (number < 0) {
  74.             number = -number;
  75.             prefix = "negative";
  76.         }
  77.        
  78.         String current = "";
  79.         int place = 0;
  80.        
  81.         do {
  82.             int n = number % 1000;
  83.             if (n != 0){
  84.                 String s = convertLessThanOneThousand(n);
  85.                 current = s + specialNames[place] + current;
  86.             }
  87.             place++;
  88.             number /= 1000;
  89.         } while (number > 0);
  90.        
  91.         return (prefix + current).trim();
  92.     }
  93.    
  94.     public static void main(String[] args) {
  95.         NumberToWord obj = new NumberToWord();
  96.         System.out.println("*** " + obj.convert(123456789));
  97.         System.out.println("*** " + obj.convert(-55));
  98.     }
  99. }
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