answersLogoWhite

0


Best Answer

//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
This answer is:
User Avatar

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 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.


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.


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.

Related questions

How do you input the number of characters in java?

".length()". The . length method is inherited from the String class.


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);


How do you import others codes in java?

using servlets, php, and database we can connect import codes into java


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.


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 is the code required to convert an integer variable to a string variable in Java?

There are several different methods to convert an integer variable to a string variable in Java. For example, one can use the following code to convert an integer variable to a string variable: Integer.toString(number)


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.


Java script program to Print the Fibonacci series using array?

&lt;script type = "text/javascript"&gt; var input; var rev = 0; input=window.prompt ("Please enter a 5-digit number to be reversed."); input = input * 1; while (input &gt; 0) { rev *= 10; rev += input % 10; input /= 10; } document.write ("Reversed number: " + rev); &lt;/script&gt;


How do you take double value input in java programming?

tamayoalfred27y.c


Can you convert date into words in java?

yes


What are good Java script codes?

depending on the intention of the javascript codes you wish to write. there is much you can do with javascript but the only way to find out what "good Java script codes" are is to explore what you want the Javascript to do on the page.