d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Spelled Numbers To Digits? Java
12Next
Add Reply New Topic New Poll
Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
Aug 4 2012 06:59pm
Anyone know of a library that can convert spelled out letters (ten) and convert them to digits (10). I'd really rather not write this out myself ;)
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Aug 4 2012 07:15pm
it takes less time to write it than it does to look for a library that does it. nobody writes libraries this trivial.

This post was edited by irimi on Aug 4 2012 07:16pm
Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
Aug 4 2012 07:29pm
I was only using 10 as an example.. I need to be able to do this with very large numbers with many decimal places..... It's not like it would take 45 seconds to write. I'm sure theres one out there
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Aug 4 2012 08:20pm
if you are so sure of that, you ought to be able to find it
Member
Posts: 4,485
Joined: Mar 24 2006
Gold: 40.00
Aug 6 2012 05:02pm
I Google'd this, so don't ask me for support. I can't even guarantee that it works.

Code

import java.text.DecimalFormat;

public class EnglishNumberToWords {

 private static final String[] tensNames = {
   "",
   " ten",
   " twenty",
   " thirty",
   " forty",
   " fifty",
   " sixty",
   " seventy",
   " eighty",
   " ninety"
 };

 private static final String[] numNames = {
   "",
   " one",
   " two",
   " three",
   " four",
   " five",
   " six",
   " seven",
   " eight",
   " nine",
   " ten",
   " eleven",
   " twelve",
   " thirteen",
   " fourteen",
   " fifteen",
   " sixteen",
   " seventeen",
   " eighteen",
   " nineteen"
 };

 private static String convertLessThanOneThousand(int number) {
   String soFar;

   if (number % 100 < 20){
     soFar = numNames[number % 100];
     number /= 100;
   }
   else {
     soFar = numNames[number % 10];
     number /= 10;

     soFar = tensNames[number % 10] + soFar;
     number /= 10;
   }
   if (number == 0) return soFar;
   return numNames[number] + " hundred" + soFar;
 }


 public static String convert(long number) {
   // 0 to 999 999 999 999
   if (number == 0) { return "zero"; }

   String snumber = Long.toString(number);

   // pad with "0"
   String mask = "000000000000";
   DecimalFormat df = new DecimalFormat(mask);
   snumber = df.format(number);

   // XXXnnnnnnnnn
   int billions = Integer.parseInt(snumber.substring(0,3));
   // nnnXXXnnnnnn
   int millions  = Integer.parseInt(snumber.substring(3,6));
   // nnnnnnXXXnnn
   int hundredThousands = Integer.parseInt(snumber.substring(6,9));
   // nnnnnnnnnXXX
   int thousands = Integer.parseInt(snumber.substring(9,12));    

   String tradBillions;
   switch (billions) {
   case 0:
     tradBillions = "";
     break;
   case 1 :
     tradBillions = convertLessThanOneThousand(billions)
     + " billion ";
     break;
   default :
     tradBillions = convertLessThanOneThousand(billions)
     + " billion ";
   }
   String result =  tradBillions;

   String tradMillions;
   switch (millions) {
   case 0:
     tradMillions = "";
     break;
   case 1 :
     tradMillions = convertLessThanOneThousand(millions)
     + " million ";
     break;
   default :
     tradMillions = convertLessThanOneThousand(millions)
     + " million ";
   }
   result =  result + tradMillions;

   String tradHundredThousands;
   switch (hundredThousands) {
   case 0:
     tradHundredThousands = "";
     break;
   case 1 :
     tradHundredThousands = "one thousand ";
     break;
   default :
     tradHundredThousands = convertLessThanOneThousand(hundredThousands)
     + " thousand ";
   }
   result =  result + tradHundredThousands;

   String tradThousand;
   tradThousand = convertLessThanOneThousand(thousands);
   result =  result + tradThousand;

   // remove extra spaces!
   return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
 }

 /**
  * testing
  * @param args
  */
 public static void main(String[] args) {
   System.out.println("*** " + EnglishNumberToWords.convert(0));
   System.out.println("*** " + EnglishNumberToWords.convert(1));
   System.out.println("*** " + EnglishNumberToWords.convert(16));
   System.out.println("*** " + EnglishNumberToWords.convert(100));
   System.out.println("*** " + EnglishNumberToWords.convert(118));
   System.out.println("*** " + EnglishNumberToWords.convert(200));
   System.out.println("*** " + EnglishNumberToWords.convert(219));
   System.out.println("*** " + EnglishNumberToWords.convert(800));
   System.out.println("*** " + EnglishNumberToWords.convert(801));
   System.out.println("*** " + EnglishNumberToWords.convert(1316));
   System.out.println("*** " + EnglishNumberToWords.convert(1000000));
   System.out.println("*** " + EnglishNumberToWords.convert(2000000));
   System.out.println("*** " + EnglishNumberToWords.convert(3000200));
   System.out.println("*** " + EnglishNumberToWords.convert(700000));
   System.out.println("*** " + EnglishNumberToWords.convert(9000000));
   System.out.println("*** " + EnglishNumberToWords.convert(9001000));
   System.out.println("*** " + EnglishNumberToWords.convert(123456789));
   System.out.println("*** " + EnglishNumberToWords.convert(2147483647));
   System.out.println("*** " + EnglishNumberToWords.convert(3000000010L));

   /*
    *** zero
    *** one
    *** sixteen
    *** one hundred
    *** one hundred eighteen
    *** two hundred
    *** two hundred nineteen
    *** eight hundred
    *** eight hundred one
    *** one thousand three hundred sixteen
    *** one million
    *** two millions
    *** three millions two hundred
    *** seven hundred thousand
    *** nine millions
    *** nine millions one thousand
    *** one hundred twenty three millions four hundred
    **      fifty six thousand seven hundred eighty nine
    *** two billion one hundred forty seven millions
    **      four hundred eighty three thousand six hundred forty seven
    *** three billion ten
    **/
 }
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 6 2012 05:13pm
^he actually wanted it the other way around
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Aug 6 2012 05:57pm
Quote (carteblanche @ Aug 6 2012 04:13pm)
^he actually wanted it the other way around


which is a lot more annoying and painful, unless you somehow restrict the grammar that you use to describe numbers as words
Member
Posts: 9,099
Joined: Nov 23 2002
Gold: 2,911.26
Aug 6 2012 11:21pm
Quote (irimi @ Aug 6 2012 07:57pm)
which is a lot more annoying and painful, unless you somehow restrict the grammar that you use to describe numbers as words


Which is INCREDIBLY painful unless you make sure it is spelled correct and proper formation of the numbers in text... such as one hundred and thirty nine... is that 139 or 100.39

Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
Aug 10 2012 08:25am
Quote (thestealthtarget @ Aug 6 2012 09:21pm)
Which is INCREDIBLY painful unless you make sure it is spelled correct and proper formation of the numbers in text... such as one hundred and thirty nine... is that 139 or 100.39


Since I couldn't find a lib for this, I wrote the damn thing out. It's not working for this exact reason. I was taught that it was only correct to use "and" in numbers when referring to the decimal. So one hundred and thirty-nine would be 100.39. Apparently this isn't universal as I have some people using several ands in one number. I also have fuckers not using hyphens in numbers like twenty-five. What a waste of time this turned out to be.
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Aug 10 2012 12:55pm
Quote (Furio @ Aug 10 2012 12:25pm)
Since I couldn't find a lib for this, I wrote the damn thing out. It's not working for this exact reason. I was taught that it was only correct to use "and" in numbers when referring to the decimal. So one hundred and thirty-nine would be 100.39. Apparently this isn't universal as I have some people using several ands in one number. I also have fuckers not using hyphens in numbers like twenty-five. What a waste of time this turned out to be.


this was kinda obvious to happen =/


Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll