answersLogoWhite

0

//By rohan 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)); /* OUTPUT WILL BE LIKE THIS *** 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 **/ } }

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
DevinDevin
I've poured enough drinks to know that people don't always want advice—they just want to talk.
Chat with Devin
TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga

Add your answer:

Earn +20 pts
Q: Convert input number to word in java codes?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why we use IntegerParseInt in java?

Parsing is very important since the input from the user is not in the form of ints but in a String, therefore, you have to parse the String containing the number into a primitive data type. i.e. String num = "49"; int realNum = Integer.parseInt(num); // puts 49 into realNum;


Which is the best way to convert numbers in string under java?

All of the Java number classes have a parse[type] method, like parseInt() in Integer or parseDouble() in Double that convert Strings to primitive numbers. String s = getInput(); int var = Integer.parseInt(s);


What is number format exception in java?

That may happen when Java tries to parse a String, to convert it into a number. In this case, if the String doesn't contain a valid number - or perhaps if it contains additional symbols not appropriate for a number - you may get this error.


What do you call a Program that runs Java byte code instruction?

Get the JDK &amp; Bluej from net and the rest will be done by them. Java byte codes are stored as *.class ; where "*" represents the class name, in your hard disk. You can download BlueJ as well as JDK from the related link.


How to write a java program to extract the last digit from the input?

Assuming you've entered a multi-digit number whole number (an integer), then take the modus (%) of the number and 10. E.g., if the number input was 1234, then 1234 % 10 is 4. Thus the final digit is 4. Note that modus 10 is the same as dividing the number by 10 and taking the remainder.