Just use String().subString() method and you can develop java code to convert number to word. Like 12345678 to
one Crore twenty three Lakh forty five thousand six hundred seventy eight.
import java.text.DecimalFormat;
public class NumberToWord {
public static void main(String[] args) {
System.out.println(convert(12345678));
}
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);
int hyndredCrore = Integer.parseInt(snumber.substring(3,5));
int hundredLakh = Integer.parseInt(snumber.substring(5,7));
int hundredThousands = Integer.parseInt(snumber.substring(7,9));
int thousands = Integer.parseInt(snumber.substring(9,12));
String tradBillions;
switch (hyndredCrore) { case 0: tradBillions = ""; break; case 1 : tradBillions = convertLessThanOneThousand(hyndredCrore) + " Crore "; break; default : tradBillions = convertLessThanOneThousand(hyndredCrore) + " Crore "; }
String result = tradBillions;
String tradMillions;
switch (hundredLakh) { case 0: tradMillions = ""; break; case 1 : tradMillions = convertLessThanOneThousand(hundredLakh) + " Lakh "; break; default : tradMillions = convertLessThanOneThousand(hundredLakh) + " Lakh "; }
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;return result.replaceAll("^\\s+", "").replaceAll("file://b//s%7B2,%7D//b", " "); }
}
Thursday, August 13, 2009
Subscribe to:
Posts (Atom)