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.");
}
}