Question:
how to convert numbers to words using java program up to 1milion?
Dean
2009-08-25 23:30:57 UTC
how to convert numbers to words using java program up to 1million. im having a problem regarding this program. plz teach me how to do it using javax swing or either other functions
Five answers:
Panqueque
2009-08-26 06:58:20 UTC
import java.util.Scanner;



public class NumberToWord {

public static final String[] DIGITS = {"one", "two", "three", "four", "five",

"six", "seven", "eight", "nine"};

public static final String[] TENS = {null, "twenty", "thirty", "forty", "fifty",

"sixty", "seventy", "eighty", "ninety"};

public static final String[] TEENS = {"ten", "eleven", "twelve", "thirteen", "fourteen",

"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};



// use for every grouping of 10^3 - thousands, millions, billions, etc.

public static String wordifyNumber(int number) {

StringBuilder sb = new StringBuilder();

int x = number / 100;

if (x > 0) { // something in hundreds column

sb.append(DIGITS[x - 1] + " hundred");

}

x = number % 100;

int tens = x / 10;

if (tens > 0) { // something in tens column

if (sb.length() > 0) {

sb.append(" ");

}

if (tens > 1) {

sb.append(TENS[tens - 1]);

} else {

sb.append(TEENS[x - 10]);

// TEENS accounts for tens + digit; done!

number = 0;

}

}

x = number % 10;

if (x > 0) { // something in digits column

if (sb.length() > 0) {

if (tens >= 2) { // twenty-, thirty-, etc.

sb.append("-");

} else {

sb.append(" ");

}

}

sb.append(DIGITS[x - 1]);

}

return sb.toString();

}



public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("Enter a number (or -1 to quit): ");

int number = scanner.nextInt();

if (number == -1) {

break;

} else if (number == 0) {

System.out.println("Number = zero");

} else {

System.out.print("Number = ");

if (number > 999) {

System.out.print(

wordifyNumber(number / 1000) + " thousand");

number = number % 1000;

System.out.print(number > 99 ? ", " : " ");

}

System.out.println(

wordifyNumber(number));

}

}

System.out.println("Done.");

}

}



*** edit - hey, who's the hater with the thumbs down? this code rocks! you people are too funny!
anonymous
2016-12-20 12:43:41 UTC
1
Mark aka jack573
2009-08-26 00:11:41 UTC
You would need to progressively build the string up. I won't say how to build it up, as you should know how.



I will give you an example of doing 1234 but in psuedocode.



The input is an int. (1234)

Get the last number. (4)

-- number = input % 10

Then you would want to remove the 4 from the number and leave 123

input = input / 10;

Add "four" to the string.



Then do the same loop as above.



One problem with this would be.

If you had the number 100.

How would you know if number = 0 above if the number of 1's is 0, or number of 10's is 0, or if the number is not greater than 100.



For instance, how to determine between 1100, and 100, so you can stop the loop.



One way in java would be to convert the number to a string. Either by

String inputAsString = "" + input;

or

String inputAsString = new String(input);



Then you could find the length of the string, and use this to loop until you get to the last number.

So, 100 in a string would have the length of 3, and 1100 in a string would have the length of 4.



So you could loop the length of the string.

for (int i = 0; i < inputAsString.length(); i++)

number = input % 10;

input = input / 10;

// use number to get the string, like 4 should come back as four.



Then you have an additional problem. If you have 14, and write four, how do you know to change it to fourteen. It could be 12. So how to check if it is twoteen (not twelve). I will leave this to you.





[Edit]

Use Panqueque's code, as it is pretty good, but you will have to modify some things in it.



Remove the line

import java.util.Scanner;



and replace it with



import javax.swing.JOptionPane;



In the main method where you see:



Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("Enter a number (or -1 to quit): ");

int number = scanner.nextInt();



Replace this with



String input = JOptionPane.showInputDialog(null, "Enter a number (or -1 to quit)");

int number;

try

{

number = Integer.parseInt(input);

}

catch (NumberFormatException e)

{

JoptionPane.showMessageDialog(null, "You did not enter an integer. " + e.getMessage());

return;

}





In the main method where you see all of the

System.out.println( ... );



Use

JOptionPane.showMessageDialog(null, );
Greiga
2009-08-25 23:57:21 UTC
String.valueOf(1000000);
Adnan
2014-08-09 13:50:12 UTC
import java.util.*;



public class Ideone



{



public void pw(int n,String ch)



{



String one[]={



" "," one"," two"," three"," four"," five"," six"," seven"," eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};



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



if(n>19) { System.out.print(ten[n/10]+" "+one[n%10]);} else { System.out.print(one[n]);}if(n>0)System.out.print(ch);



}



public static void main(String[] args)



{



int n=0;



Scanner s =new Scanner(System.in);



System.out.print("Enter an integer number: ");



n = s.nextInt();



if(n<=0)



System.out.print("Enter numbers greater than 0");



else



{



Ideone a =new Ideone();



System.out.print("After conversion number in words is :");



a.pw((n/1000000000)," Hundred");



a.pw((n/10000000)%100," crore");



a.pw(((n/100000)%100)," lakh");



a.pw(((n/1000)%100)," thousand");



a.pw(((n/100)%10)," hundred");



a.pw((n%100)," ");



}



}



}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...