We currently have a crude mechanism to convert numbers to words (e.g. using a few static arrays) and based on the size of the number translating that into an english text. B
The same accepted answer (Jigar Joshi), but now in Spanish. Feel free to change this if you find a mistake. Easier than french, but based on that though....
Spanish:
import java.text.*;
class SpanishNumberToWords {
private static final String[] tensNames = {
"",
"",
"veinte",
"treinta",
"cuarenta",
"cincuenta",
"sesenta",
"setenta",
"ochenta",
"noventa"
};
private static final String[] unitNames1 = {
"",
"un",
"dos",
"tres",
"cuatro",
"cinco",
"seis",
"siete",
"ocho",
"nueve",
"diez",
"once",
"doce",
"trece",
"catorce",
"quince",
"dieciseis",
"diecisiete",
"dieciocho",
"diecinueve",
"veinte",
"veintiun",
"veintidos",
"veintitres",
"veinticuatro",
"veinticinco",
"veintiseis",
"veintisiete",
"veintiocho",
"veintinueve",
};
private static final String[] unitNames2 = {
"",
"",
"dosc",
"tresc",
"cuatroc",
"quin",
"seisc",
"setec",
"ochoc",
"novec",
"diez"
};
private SpanishNumberToWords() {}
private static String convertZeroToHundred(int number) {
int theTens = number / 10;
int theUnit = number % 10;
String result = "";
// separator
String theSeparator = "";
if (theTens > 1) {
theSeparator = " y ";
}
// particular cases
switch (theUnit) {
case 0:
theSeparator = "";
break;
default:
}
// tens in letters
switch (theTens) {
case 0:
result = unitNames1[theUnit];
break;
case 1: case 2:
result = unitNames1[theTens*10+theUnit];
break;
default :
result = tensNames[theTens]
+ theSeparator + unitNames1[theUnit];
}
return result;
}
private static String convertLessThanOneThousand(int number) {
int theHundreds = number / 100;
int leReste = number % 100;
String sReste = convertZeroToHundred(leReste);
String result;
switch (theHundreds) {
case 0:
result = sReste;
break;
case 1 :
if (leReste > 0) {
result = "ciento " + sReste;
}
else {
result = "cien";
}
break;
default :
if (leReste > 0) {
result = unitNames2[theHundreds] + "ientos " + sReste;
}
else {
result = unitNames2[theHundreds] + "ientos";
}
}
return result;
}
public static String convert(long number) {
// 0 à 999 999 999 999
if (number == 0) { return "cero"; }
String snumber = Long.toString(number);
// pad des "0"
String mask = "000000000000";
DecimalFormat df = new DecimalFormat(mask);
snumber = df.format(number);
// XXXnnnnnnnnn
int theMilliards = Integer.parseInt(snumber.substring(0,3));
// nnnXXXnnnnnn
int theMillions = Integer.parseInt(snumber.substring(3,6));
// nnnnnnXXXnnn
int theCentMiles = Integer.parseInt(snumber.substring(6,9));
// nnnnnnnnnXXX
int lesMille = Integer.parseInt(snumber.substring(9,12));
String tradMilliards;
switch (theMilliards) {
case 0:
tradMilliards = "";
break;
case 1 :
tradMilliards = convertLessThanOneThousand(theMilliards)
+ " mil millones ";
break;
default :
tradMilliards = convertLessThanOneThousand(theMilliards)
+ " mil millones ";
}
String resultat = tradMilliards;
String tradMillions;
switch (theMillions) {
case 0:
tradMillions = "";
break;
case 1 :
tradMillions = convertLessThanOneThousand(theMillions)
+ " millon ";
break;
default :
tradMillions = convertLessThanOneThousand(theMillions)
+ " millones ";
}
resultat = resultat + tradMillions;
String tradCentMille;
switch (theCentMiles) {
case 0:
tradCentMille = "";
break;
case 1 :
tradCentMille = "mil ";
break;
default :
tradCentMille = convertLessThanOneThousand(theCentMiles)
+ " mil ";
}
resultat = resultat + tradCentMille;
String tradMille;
tradMille = convertLessThanOneThousand(lesMille);
resultat = resultat + tradMille;
return resultat;
}
public static void main(String[] args) {
System.out.println("*** " + SpanishNumberToWords.convert(0));
System.out.println("*** " + SpanishNumberToWords.convert(1));
System.out.println("*** " + SpanishNumberToWords.convert(2));
System.out.println("*** " + SpanishNumberToWords.convert(3));
System.out.println("*** " + SpanishNumberToWords.convert(4));
System.out.println("*** " + SpanishNumberToWords.convert(5));
System.out.println("*** " + SpanishNumberToWords.convert(7));
System.out.println("*** " + SpanishNumberToWords.convert(12));
System.out.println("*** " + SpanishNumberToWords.convert(16));
System.out.println("*** " + SpanishNumberToWords.convert(19));
System.out.println("*** " + SpanishNumberToWords.convert(21));
System.out.println("*** " + SpanishNumberToWords.convert(24));
System.out.println("*** " + SpanishNumberToWords.convert(28));
System.out.println("*** " + SpanishNumberToWords.convert(29));
System.out.println("*** " + SpanishNumberToWords.convert(30));
System.out.println("*** " + SpanishNumberToWords.convert(31));
System.out.println("*** " + SpanishNumberToWords.convert(42));
System.out.println("*** " + SpanishNumberToWords.convert(71));
System.out.println("*** " + SpanishNumberToWords.convert(72));
System.out.println("*** " + SpanishNumberToWords.convert(80));
System.out.println("*** " + SpanishNumberToWords.convert(81));
System.out.println("*** " + SpanishNumberToWords.convert(89));
System.out.println("*** " + SpanishNumberToWords.convert(90));
System.out.println("*** " + SpanishNumberToWords.convert(91));
System.out.println("*** " + SpanishNumberToWords.convert(97));
System.out.println("*** " + SpanishNumberToWords.convert(100));
System.out.println("*** " + SpanishNumberToWords.convert(101));
System.out.println("*** " + SpanishNumberToWords.convert(110));
System.out.println("*** " + SpanishNumberToWords.convert(120));
System.out.println("*** " + SpanishNumberToWords.convert(200));
System.out.println("*** " + SpanishNumberToWords.convert(201));
System.out.println("*** " + SpanishNumberToWords.convert(232));
System.out.println("*** " + SpanishNumberToWords.convert(999));
System.out.println("*** " + SpanishNumberToWords.convert(521));
System.out.println("*** " + SpanishNumberToWords.convert(912));
System.out.println("*** " + SpanishNumberToWords.convert(999));
System.out.println("*** " + SpanishNumberToWords.convert(1000));
System.out.println("*** " + SpanishNumberToWords.convert(1001));
System.out.println("*** " + SpanishNumberToWords.convert(10000));
System.out.println("*** " + SpanishNumberToWords.convert(10001));
System.out.println("*** " + SpanishNumberToWords.convert(100000));
System.out.println("*** " + SpanishNumberToWords.convert(267578));
System.out.println("*** " + SpanishNumberToWords.convert(3000000000L));
System.out.println("*** " + SpanishNumberToWords.convert(2147483647));
/*
*** OUTPUT
*** cero
*** un
*** dos
*** tres
*** cuatro
*** cinco
*** siete
*** doce
*** dieciseis
*** diecinueve
*** veintiun
*** veinticuatro
*** veintiocho
*** veintinueve
*** treinta
*** treinta y un
*** cuarenta y dos
*** setenta y un
*** setenta y dos
*** ochenta
*** ochenta y un
*** ochenta y nueve
*** noventa
*** noventa y un
*** noventa y siete
*** cien
*** ciento un
*** ciento diez
*** ciento veinte
*** doscientos
*** doscientos un
*** doscientos treinta y dos
*** novecientos noventa y nueve
*** quinientos veintiun
*** novecientos doce
*** novecientos noventa y nueve
*** mil
*** mil un
*** diez mil
*** diez mil un
*** cien mil
*** doscientos sesenta y siete mil quinientos setenta y ocho
*** tres mil millones
*** dos mil millones ciento cuarenta y siete millones cuatrocientos ochenta y tres mil seiscientos cuarenta y siete
*/
}
/**
This Program will display the given number in words from 0 to 999999999
@author Manoj Kumar Dunna
Mail Id : manojdunna@gmail.com
**/
import java.util.Scanner;
class NumberToString
{
public enum hundreds {OneHundred, TwoHundred, ThreeHundred, FourHundred, FiveHundred, SixHundred, SevenHundred, EightHundred, NineHundred}
public enum tens {Twenty, Thirty, Forty, Fifty, Sixty, Seventy, Eighty, Ninety}
public enum ones {One, Two, Three, Four, Five, Six, Seven, Eight, Nine}
public enum denom {Thousand, Lakhs, Crores}
public enum splNums { Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen, Seventeen, Eighteen, Nineteen}
public static String text = "";
public static void main(String[] args)
{
System.out.println("Enter Number to convert into words");
Scanner sc = new Scanner(System.in);
long num = sc.nextInt();
int rem = 0;
int i = 0;
while(num > 0)
{
if(i == 0){
rem = (int) (num % 1000);
printText(rem);
num = num / 1000;
i++;
}
else if(num > 0)
{
rem = (int) (num % 100);
if(rem > 0)
text = denom.values()[i - 1]+ " " + text;
printText(rem);
num = num / 100;
i++;
}
}
if(i > 0)
System.out.println(text);
else
System.out.println("Zero");
}
public static void printText(int num)
{
if(!(num > 9 && num < 19))
{
if(num % 10 > 0)
getOnes(num % 10);
num = num / 10;
if(num % 10 > 0)
getTens(num % 10);
num = num / 10;
if(num > 0)
getHundreds(num);
}
else
{
getSplNums(num % 10);
}
}
public static void getSplNums(int num)
{
text = splNums.values()[num]+ " " + text;
}
public static void getHundreds(int num)
{
text = hundreds.values()[num - 1]+ " " + text;
}
public static void getTens(int num)
{
text = tens.values()[num - 2]+ " " + text;
}
public static void getOnes(int num)
{
text = ones.values()[num - 1]+ " " + text;
}
}
this might help
public String numberToWords(long number) {
if (number == 0) {
return "zero";
}
if (number < 0) {
return "minus " + numberToWords(Math.abs(number));
}
String words = "";
if ((number / 10000000) > 0) {
words += numberToWords(number / 10000000) + " Crore ";
number %= 10000000;
}
if ((number / 100000) > 0) {
words += numberToWords(number / 100000) + " Lakh ";
number %= 100000;
}
if ((number / 1000) > 0) {
words += numberToWords(number / 1000) + " Thousand ";
number %= 1000;
}
if ((number / 100) > 0) {
words += numberToWords(number / 100) + " Hundred ";
number %= 100;
}
if (number > 0) {
if (!words.equals("")) {
words += "and ";
}
if (number < 20) {
words += number;
} else {
words += (number);
if ((number % 10) > 0) {
words += "-" + (number % 10);
}
}
}
return words;
}
I have used 2 dimensional array...
import java.util.Scanner;
public class numberEnglish {
public static void main(String args[])
{
String[ ][ ] aryNumbers = new String[9][4];
aryNumbers[0][0] = "one";
aryNumbers[0][1] = "ten";
aryNumbers[0][2] = "one hundred and";
aryNumbers[0][3] = "one thousand";
aryNumbers[1][0] = "two";
aryNumbers[1][1] = "twenty";
aryNumbers[1][2] = "two hundred and";
aryNumbers[1][3] = "two thousand";
aryNumbers[2][0] = "three";
aryNumbers[2][1] = "thirty";
aryNumbers[2][2] = "three hundred and";
aryNumbers[2][3] = "three thousand";
aryNumbers[3][0] = "four";
aryNumbers[3][1] = "fourty";
aryNumbers[3][2] = "four hundred and";
aryNumbers[3][3] = "four thousand";
aryNumbers[4][0] = "five";
aryNumbers[4][1] = "fifty";
aryNumbers[4][2] = "five hundred and";
aryNumbers[4][3] = "five thousand";
aryNumbers[5][0] = "six";
aryNumbers[5][1] = "sixty";
aryNumbers[5][2] = "six hundred and";
aryNumbers[5][3] = "six thousand";
aryNumbers[6][0] = "seven";
aryNumbers[6][1] = "seventy";
aryNumbers[6][2] = "seven hundred and";
aryNumbers[6][3] = "seven thousand";
aryNumbers[7][0] = "eight";
aryNumbers[7][1] = "eighty";
aryNumbers[7][2] = "eight hundred and";
aryNumbers[7][3] = "eight thousand";
aryNumbers[8][0] = "nine";
aryNumbers[8][1] = "ninty";
aryNumbers[8][2] = "nine hundred and";
aryNumbers[8][3] = "nine thousand";
//System.out.println(aryNumbers[0] + " "+aryNumbers[0] + " ");
int number=0;
Scanner sc = new Scanner(System.in);
System.out.println(" Enter Number 4 digited number:: ");
number = sc.nextInt();
int temp = number;
int count=1;
String english="";
String tenglish = "";
if(number == 0)
{
System.out.println("*********");
System.out.println("Zero");
System.out.println("*********");
sc.close();
return;
}
while(temp !=0)
{
int r = temp%10;
if(r==0)
{
tenglish = " zero ";
count++;
}
else
{
int t1=r-1;
int t2 = count-1;
//System.out.println(t1 +" "+t2);
count++;
tenglish = aryNumbers[t1][t2];
//System.out.println(aryNumbers[t1][t2]);
}
english = tenglish +" "+ english;
temp = temp/10;
}
//System.out.println(aryNumbers[0][0]);
english = english.replace("ten zero", "ten");
english = english.replace("twenty zero", "twenty");
english = english.replace("thirty zero", "thirty");
english = english.replace("fourty zero", "fourty");
english = english.replace("fifty zero", "fifty");
english = english.replace("sixty zero", "sixty");
english = english.replace("seventy zero", "seventy");
english = english.replace("eighty zero", "eighty");
english = english.replace("ninety zero", "ninety");
english = english.replace("ten one", "eleven");
english = english.replace("ten two", "twelve");
english = english.replace("ten three", "thirteen");
english = english.replace("ten four", "fourteen");
english = english.replace("ten five", "fifteen");
english = english.replace("ten six", "sixteen");
english = english.replace("ten seven", "seventeen");
english = english.replace("ten eight", "eighteen");
english = english.replace("ten nine", "nineteen");
english = english.replace(" zero ", "");
int length = english.length();
String sub = english.substring(length-6,length-3);
//System.out.println(length);
//System.out.println(sub);
if(sub.equals("and"))
{
//System.out.println("hello");
english=english.substring(0,length-6);
}
System.out.println("********************************************");
System.out.println(english);
System.out.println("********************************************");
sc.close();
}
}
I think this may help you...programme is very simple and works fine
import java.util.*;
public class NumberToWord
{
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
{
NumberToWord a = new NumberToWord();
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), " ");
}
}
}
I implemented it like this
package com.stack.overflow.number.in.english;
import java.util.ResourceBundle;
public class ActualImplementation {
public static ResourceBundle readPropertyFile = ResourceBundle
.getBundle("NumberEnglishRepresentation");
public static void main(String[] args) {
System.out.println(ActualImplementation.main(-2));
}
public static String main(Integer number) {
int power;
// Calculate Number of digits
Integer numberOfDigits = number > 0 ? (int) Math.log10((double) number) + 1
: 1;
String output = "";
// If number is negative convert it to positive an append minus to
// output
if (Integer.signum(number) == -1) {
output = "minus ";
number = number < 0 ? number * -1 : number;
}
String stringVal = String.valueOf(number);
if (number <= 20 || number == 30 || number == 40 || number == 50
|| number == 60 || number == 70 || number == 80 || number == 90
|| number == 100 || number == 1000)
output += readPropertyFile.getString(stringVal);
else {
int i;
for (i = 0; i < numberOfDigits; i++) {
if (number != 0) {
numberOfDigits = number > 0 ? (int) Math
.log10((double) number) + 1 : 1;
power = (int) Math.pow(10, numberOfDigits - 1);
// If number is like 10,001 then print ten first and then
// remaining value
if (numberOfDigits >= 5 && numberOfDigits % 2 == 1) {
power = (int) Math.pow(10, numberOfDigits - 2);
}
if (readPropertyFile.containsKey(String.valueOf(number)))
output += readPropertyFile.getString(String
.valueOf(number));
else {
// As the digits at units and tens place are read
// differently
if (numberOfDigits > 2) {
output += readPropertyFile.getString(String
.valueOf(number / power))
+ readPropertyFile.getString(String
.valueOf(power));
} else {
output += readPropertyFile.getString(String
.valueOf(number - number % power));
}
}
number = (int) (number % power);
}
}
}
return output;
}
}
and the resource file is :
0=zero
1=one
2=two
3=three
4=four
5=five
6=six
7=seven
8=eight
9=nine
10=ten
11=eleven
12=twelve
13=thirteen
14=fourteen
15fifteen
16=sixteen
17=seventeen
18=eighteen
19=nineteen
20=twenty
30=thirty
40=fourty
50=fifty
60=sixty
70=seventy
80=eighty
90=ninety
100=hundred
1000=thousand
100000=Lakh
This one is implemented till 10 lakhs only