hey guys i did a program for converting number into words and i almost got through but my folks want it even possible for decimal numbers too buti couldnt make that possible and can anyone help me in this and i also attach the codes i did and if something is wrong let me know it

Thanks in advance guys:):):)

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"};
	public static String wordifyNumber(int number) 
	{
		StringBuilder sb = new StringBuilder();
		int x = number / 100;
		if (x > 0) 
	{ 
		sb.append(DIGITS[x - 1] + " hundred");
	}
		x = number % 100;
		int tens = x / 10;
		if (tens > 0) 
	{ 
		if (sb.length() > 0) 
	{
		sb.append(" ");
	}
		if (tens > 1) 
	{
		sb.append(TENS[tens - 1]);
	} 
		else 
	{
		sb.append(TEENS[x - 10]);
		number = 0;
	}
	}
		x = number % 10;
		if (x > 0) 
	{ 
		if (sb.length() > 0) 
	{
		if (tens >= 2) 
	{
		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.");
	}
}

Why can't you do it for decimal numbers? the logic is about the same, but you'll just need to accept a numeric type that is capable of taking decimal numbers, like float or double, and take into consideration the number of leading zeroes in your decimal part, too.

stultuske i did the same thing using double data type but i am getting error in all the append lines in the program and i tried all possible ways and i m out of ideas so seeking help from u guys...if possible u try replacing int to double...

well, how about you show what you tried that didn't work. it'll be easier for us to determine if and (if so) what you did wrong.

[stultuske this is what i tried and edited things as i told you earlier

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"};
	public static String wordifyNumber(double number) 
	{
		StringBuilder sb = new StringBuilder();
		double x = number / 100;
		if (x > 0) 
	{ 
		sb.append(DIGITS[(int)((double)x - 1)] + " hundred");
	}
		x = number % 100;
		double tens = x / 10;
		if (tens > 0) 
	{ 
		if (sb.length() > 0) 
	{
		sb.append(" ");
	}
		if (tens > 1) 
	{
		sb.append(TENS[(int)((double)tens - 1)]);
	} 
		else 
	{
		sb.append(TEENS[(int)((double)x - 10)]);
		number = 0;
	}
	}
		x = number % 10;
		if (x > 0) 
	{ 
		if (sb.length() > 0) 
	{
		if (tens >= 2) 
	{
		sb.append("-");
	} 
		else 
	{
		sb.append(" ");
	}
	}
		sb.append(DIGITS[(int)((double)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): ");
		double number = scanner.nextDouble();
		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.");
	}
}

well, I get ArrayIndexOutOfBounds 49 and stuff like that, so you may want to recheck your code a bit.

stultuske i dont understand what your saying and i could execute it for some random nos but not all nos.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.