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
This program can convert up to 102 digits long number. Suggestions and comments are highly appreciated.
package com.kegeesoft;
/**
* @author Chandana Gamage +94 710 980 120
* @author maheshgamage375@gmail.com
* @author KeGee Software Solutions
*/
import java.math.BigDecimal;
import java.math.BigInteger;
public class Converter {
private final BigInteger zero = new BigInteger("0");
private final BigInteger scale[] = new BigInteger[33];
private final String scaleName[] = {" Duotrigintillion"," Untrigintillion"," Trigintillion",
" Nonvigintillion"," Octovigintillion"," Septvigintillion",
" Sexvigintillion"," Quinvigintillion"," Quattuorvigintillion",
" Trevigintillion"," Duovigintillion"," Unvigintillion",
" Vigintillion"," Novemdecillion"," Octodecillion",
" Septemdecillion"," Sexdecillion"," Quindecillion",
" Quattuordecillion "," Tredecillion"," Duodecillion",
" Undecillion"," Decillion"," Nonillion"," Octillion",
" Septillion"," Sextillion"," Quintillion"," Quadrillion",
" Trillion"," Billion"," Million"," Thousand"};
private final String ones[] = {""," One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight"," Nine",
" Ten"," Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen",
" Seventeen"," Eighteen"," Nineteen"};
private final String tens[] = {"",""," Twenty"," Thirty"," Forty"," Fifty"," Sixty"," Seventy"," Eighty"," Ninety"};
private int index = 0;
private String shortValueInWords = "";
private String output = "";
private String decimalInWords = "";
private String valueInWords;
public String setValue(BigInteger value) throws Exception{
return this.bigValueInWords(value);
}
public String setValue(BigDecimal value) throws Exception{
int indexOfDecimalPoint = (value.toString()).indexOf(".");
// Split and pass interger value of given decimal value to constructor
String tempIntValue = (value.toString()).substring(0, indexOfDecimalPoint);
BigInteger intValue = new BigInteger(tempIntValue);
// Split and pass decimal value of given decimal value to constructor
String tempDeciValue = (value.toString()).substring(indexOfDecimalPoint+1, value.toString().length());
BigInteger deciValue = new BigInteger(tempDeciValue);
this.bigValueInWords(intValue);
this.decimalValueInWords(deciValue);
return null;
}
public String setValue(BigDecimal value, String currencyName, String centsName) throws Exception{
int indexOfDecimalPoint = (value.toString()).indexOf(".");
// Split and pass interger value of given decimal value to constructor
String tempIntValue = (value.toString()).substring(0, indexOfDecimalPoint);
BigInteger intValue = new BigInteger(tempIntValue);
// Split and pass decimal value of given decimal value to constructor
String tempDeciValue = (value.toString()).substring(indexOfDecimalPoint+1, value.toString().length());
@SuppressWarnings("UnusedAssignment")
BigInteger deciValue = null;
// Concatenate "0" if decimal value has only single digit
if(tempDeciValue.length() == 1){
deciValue = new BigInteger(tempDeciValue.concat("0"));
}else{
deciValue = new BigInteger(tempDeciValue);
}
this.output = currencyName+" ";
this.bigValueInWords(intValue);
this.centsValueInWords(deciValue, centsName);
return null;
}
private String bigValueInWords(BigInteger value) throws Exception{
// Build scale array
int exponent = 99;
for (int i = 0; i < scale.length; i++) {
scale[i] = new BigInteger("10").pow(exponent);
exponent = exponent - 3;
}
/* Idntify whether given value is a minus value or not
if == yes then pass value without minus sign
and pass Minus word to output
*/
if(value.compareTo(zero) == -1){
value = new BigInteger(value.toString().substring(1,value.toString().length()));
output += "Minus ";
}
// Get value in words of big numbers (duotrigintillions to thousands)
for (int i=0; i < scale.length; i++) {
if((value.divide(scale[i])).compareTo(zero)==1){
this.index = (int)(value.divide(scale[i])).intValue();
output += shortValueInWords(this.index) + scaleName[i];
value = value.mod(scale[i]);
}
}
// Get value in words of short numbers (hundreds, tens and ones)
output += shortValueInWords((int)(value.intValue()));
// Get rid of any space at the beginning of output and return value in words
return this.valueInWords = output.replaceFirst("\\s", "");
}
private String shortValueInWords(int shortValue) throws Exception{
// Get hundreds
if(String.valueOf(shortValue).length()==3){
shortValueInWords = ones[shortValue / 100]+" Hundred"+shortValueInWords(shortValue % 100);
}
// Get tens
if(String.valueOf(shortValue).length()== 2 && shortValue >= 20){
if((shortValue / 10)>=2 && (shortValue % 10)>=0){
shortValueInWords = tens[shortValue / 10] + ones[shortValue % 10];
}
}
// Get tens between 10 and 20
if(String.valueOf(shortValue).length()== 2 && shortValue >= 10 && shortValue < 20){
shortValueInWords = ones[shortValue];
}
// Get ones
if(String.valueOf(shortValue).length()==1){
shortValueInWords = ones[shortValue];
}
return this.shortValueInWords;
}
private String decimalValueInWords(BigInteger decimalValue) throws Exception{
decimalInWords = " Point";
// Get decimals in point form (0.563 = zero point five six three)
for(int i=0; i < (decimalValue.toString().length()); i++){
decimalInWords += ones[Integer.parseInt(String.valueOf(decimalValue.toString().charAt(i)))];
}
return this.decimalInWords;
}
private String centsValueInWords(BigInteger decimalValue, String centsName) throws Exception{
decimalInWords = " and"+" "+centsName;
// Get cents in words (5.52 = five and cents fifty two)
if(decimalValue.intValue() == 0){
decimalInWords += shortValueInWords(decimalValue.intValue())+" Zero only";
}else{
decimalInWords += shortValueInWords(decimalValue.intValue())+" only";
}
return this.decimalInWords;
}
public String getValueInWords(){
return this.valueInWords + decimalInWords;
}
public static void main(String args[]){
Converter c1 = new Converter();
Converter c2 = new Converter();
Converter c3 = new Converter();
Converter c4 = new Converter();
try{
// Get integer value in words
c1.setValue(new BigInteger("15634886"));
System.out.println(c1.getValueInWords());
// Get minus integer value in words
c2.setValue(new BigInteger("-15634886"));
System.out.println(c2.getValueInWords());
// Get decimal value in words
c3.setValue(new BigDecimal("358621.56895"));
System.out.println(c3.getValueInWords());
// Get currency value in words
c4.setValue(new BigDecimal("358621.56"),"Dollar","Cents");
System.out.println(c4.getValueInWords());
}catch(Exception e){
e.printStackTrace();
}
}
}
You can use ICU4J, Just need to add POM entry and code is below for any Number, Country and Language.
POM Entry
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>64.2</version>
</dependency>
Code is
public class TranslateNumberToWord {
/**
* Translate
*
* @param ctryCd
* @param lang
* @param reqStr
* @param fractionUnitName
* @return
*/
public static String translate(String ctryCd, String lang, String reqStr, String fractionUnitName) {
StringBuffer result = new StringBuffer();
Locale locale = new Locale(lang, ctryCd);
Currency crncy = Currency.getInstance(locale);
String inputArr[] = StringUtils.split(new BigDecimal(reqStr).abs().toPlainString(), ".");
RuleBasedNumberFormat rule = new RuleBasedNumberFormat(locale, RuleBasedNumberFormat.SPELLOUT);
int i = 0;
for (String input : inputArr) {
CurrencyAmount crncyAmt = new CurrencyAmount(new BigDecimal(input), crncy);
if (i++ == 0) {
result.append(rule.format(crncyAmt)).append(" " + crncy.getDisplayName() + " and ");
} else {
result.append(rule.format(crncyAmt)).append(" " + fractionUnitName + " ");
}
}
return result.toString();
}
public static void main(String[] args) {
String ctryCd = "US";
String lang = "en";
String input = "95.17";
String result = translate(ctryCd, lang, input, "Cents");
System.out.println("Input: " + input + " result: " + result);
}}
Tested with quite a big number and output would be
Input: 95.17 result: ninety-five US Dollar and seventeen Cents
Input: 999999999999999999.99 result: nine hundred ninety-nine quadrillion nine hundred ninety-nine trillion nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine US Dollar and ninety-nine Cents
In this post i have just update Yanick Rochon's code. I have make it workable with lower version of java 1.6 and i was getting the output for 1.00 = one and hundredth. So i have update the code. New i get the output for 1.00 = one and zero hundredth.
I don't not what should i do. Add a new answer or edit that post. As the answer is highly ranked so i have made a new post with updating the code. I have just change this two things have mention above.
/**
* This class will convert numeric values into an english representation
*
* For units, see : http://www.jimloy.com/math/billion.htm
*
* @author yanick.rochon@gmail.com
*/
public class NumberToWords {
static public class ScaleUnit {
private int exponent;
private String[] names;
private ScaleUnit(int exponent, String... names) {
this.exponent = exponent;
this.names = names;
}
public int getExponent() {
return exponent;
}
public String getName(int index) {
return names[index];
}
}
/**
* See http://www.wordiq.com/definition/Names_of_large_numbers
*/
static private ScaleUnit[] SCALE_UNITS = new ScaleUnit[] {
new ScaleUnit(63, "vigintillion", "decilliard"),
new ScaleUnit(60, "novemdecillion", "decillion"),
new ScaleUnit(57, "octodecillion", "nonilliard"),
new ScaleUnit(54, "septendecillion", "nonillion"),
new ScaleUnit(51, "sexdecillion", "octilliard"),
new ScaleUnit(48, "quindecillion", "octillion"),
new ScaleUnit(45, "quattuordecillion", "septilliard"),
new ScaleUnit(42, "tredecillion", "septillion"),
new ScaleUnit(39, "duodecillion", "sextilliard"),
new ScaleUnit(36, "undecillion", "sextillion"),
new ScaleUnit(33, "decillion", "quintilliard"),
new ScaleUnit(30, "nonillion", "quintillion"),
new ScaleUnit(27, "octillion", "quadrilliard"),
new ScaleUnit(24, "septillion", "quadrillion"),
new ScaleUnit(21, "sextillion", "trilliard"),
new ScaleUnit(18, "quintillion", "trillion"),
new ScaleUnit(15, "quadrillion", "billiard"),
new ScaleUnit(12, "trillion", "billion"),
new ScaleUnit(9, "billion", "milliard"),
new ScaleUnit(6, "million", "million"),
new ScaleUnit(3, "thousand", "thousand"),
new ScaleUnit(2, "hundred", "hundred"),
// new ScaleUnit(1, "ten", "ten"),
// new ScaleUnit(0, "one", "one"),
new ScaleUnit(-1, "tenth", "tenth"), new ScaleUnit(-2, "hundredth", "hundredth"),
new ScaleUnit(-3, "thousandth", "thousandth"),
new ScaleUnit(-4, "ten-thousandth", "ten-thousandth"),
new ScaleUnit(-5, "hundred-thousandth", "hundred-thousandth"),
new ScaleUnit(-6, "millionth", "millionth"),
new ScaleUnit(-7, "ten-millionth", "ten-millionth"),
new ScaleUnit(-8, "hundred-millionth", "hundred-millionth"),
new ScaleUnit(-9, "billionth", "milliardth"),
new ScaleUnit(-10, "ten-billionth", "ten-milliardth"),
new ScaleUnit(-11, "hundred-billionth", "hundred-milliardth"),
new ScaleUnit(-12, "trillionth", "billionth"),
new ScaleUnit(-13, "ten-trillionth", "ten-billionth"),
new ScaleUnit(-14, "hundred-trillionth", "hundred-billionth"),
new ScaleUnit(-15, "quadrillionth", "billiardth"),
new ScaleUnit(-16, "ten-quadrillionth", "ten-billiardth"),
new ScaleUnit(-17, "hundred-quadrillionth", "hundred-billiardth"),
new ScaleUnit(-18, "quintillionth", "trillionth"),
new ScaleUnit(-19, "ten-quintillionth", "ten-trillionth"),
new ScaleUnit(-20, "hundred-quintillionth", "hundred-trillionth"),
new ScaleUnit(-21, "sextillionth", "trilliardth"),
new ScaleUnit(-22, "ten-sextillionth", "ten-trilliardth"),
new ScaleUnit(-23, "hundred-sextillionth", "hundred-trilliardth"),
new ScaleUnit(-24, "septillionth", "quadrillionth"),
new ScaleUnit(-25, "ten-septillionth", "ten-quadrillionth"),
new ScaleUnit(-26, "hundred-septillionth", "hundred-quadrillionth"), };
static public enum Scale {
SHORT, LONG;
public String getName(int exponent) {
for (ScaleUnit unit : SCALE_UNITS) {
if (unit.getExponent() == exponent) {
return unit.getName(this.ordinal());
}
}
return "";
}
}
/**
* Change this scale to support American and modern British value (short scale) or Traditional
* British value (long scale)
*/
static public Scale SCALE = Scale.SHORT;
static abstract public class AbstractProcessor {
static protected final String SEPARATOR = " ";
static protected final int NO_VALUE = -1;
protected List<Integer> getDigits(long value) {
ArrayList<Integer> digits = new ArrayList<Integer>();
if (value == 0) {
digits.add(0);
} else {
while (value > 0) {
digits.add(0, (int) value % 10);
value /= 10;
}
}
return digits;
}
public String getName(long value) {
return getName(Long.toString(value));
}
public String getName(double value) {
return getName(Double.toString(value));
}
abstract public String getName(String value);
}
static public class UnitProcessor extends AbstractProcessor {
static private final String[] TOKENS = new String[] { "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
@Override
public String getName(String value) {
StringBuilder buffer = new StringBuilder();
int offset = NO_VALUE;
int number;
if (value.length() > 3) {
number = Integer.valueOf(value.substring(value.length() - 3), 10);
} else {
number = Integer.valueOf(value, 10);
}
number %= 100;
if (number < 10) {
offset = (number % 10) - 1;
// number /= 10;
} else if (number < 20) {
offset = (number % 20) - 1;
// number /= 100;
}
if (offset != NO_VALUE && offset < TOKENS.length) {
buffer.append(TOKENS[offset]);
}
return buffer.toString();
}
}
static public class TensProcessor extends AbstractProcessor {
static private final String[] TOKENS = new String[] { "twenty", "thirty", "fourty",
"fifty", "sixty", "seventy", "eighty", "ninety" };
static private final String UNION_SEPARATOR = "-";
private UnitProcessor unitProcessor = new UnitProcessor();
@Override
public String getName(String value) {
StringBuilder buffer = new StringBuilder();
boolean tensFound = false;
int number;
if (value.length() > 3) {
number = Integer.valueOf(value.substring(value.length() - 3), 10);
} else {
number = Integer.valueOf(value, 10);
}
number %= 100; // keep only two digits
if (number >= 20) {
buffer.append(TOKENS[(number / 10) - 2]);
number %= 10;
tensFound = true;
} else {
number %= 20;
}
if (number != 0) {
if (tensFound) {
buffer.append(UNION_SEPARATOR);
}
buffer.append(unitProcessor.getName(number));
}
return buffer.toString();
}
}
static public class HundredProcessor extends AbstractProcessor {
private int EXPONENT = 2;
private UnitProcessor unitProcessor = new UnitProcessor();
private TensProcessor tensProcessor = new TensProcessor();
@Override
public String getName(String value) {
StringBuilder buffer = new StringBuilder();
int number;
if ("".equals(value)) {
number = 0;
} else if (value.length() > 4) {
number = Integer.valueOf(value.substring(value.length() - 4), 10);
} else {
number = Integer.valueOf(value, 10);
}
number %= 1000; // keep at least three digits
if (number >= 100) {
buffer.append(unitProcessor.getName(number / 100));
buffer.append(SEPARATOR);
buffer.append(SCALE.getName(EXPONENT));
}
String tensName = tensProcessor.getName(number % 100);
if (!"".equals(tensName) && (number >= 100)) {
buffer.append(SEPARATOR);
}
buffer.append(tensName);
return buffer.toString();
}
}
static public class CompositeBigProcessor extends AbstractProcessor {
private HundredProcessor hundredProcessor = new HundredProcessor();
private AbstractProcessor lowProcessor;
private int exponent;
public CompositeBigProcessor(int exponent) {
if (exponent <= 3) {
lowProcessor = hundredProcessor;
} else {
lowProcessor = new CompositeBigProcessor(exponent - 3);
}
this.exponent = exponent;
}
public String getToken() {
return SCALE.getName(getPartDivider());
}
protected AbstractProcessor getHighProcessor() {
return hundredProcessor;
}
protected AbstractProcessor getLowProcessor() {
return lowProcessor;
}
public int getPartDivider() {
return exponent;
}
@Override
public String getName(String value) {
StringBuilder buffer = new StringBuilder();
String high, low;
if (value.length() < getPartDivider()) {
high = "";
low = value;
} else {
int index = value.length() - getPartDivider();
high = value.substring(0, index);
low = value.substring(index);
}
String highName = getHighProcessor().getName(high);
String lowName = getLowProcessor().getName(low);
if (!"".equals(highName)) {
buffer.append(highName);
buffer.append(SEPARATOR);
buffer.append(getToken());
if (!"".equals(lowName)) {
buffer.append(SEPARATOR);
}
}
if (!"".equals(lowName)) {
buffer.append(lowName);
}
return buffer.toString();
}
}
static public class DefaultProcessor extends AbstractProcessor {
static private String MINUS = "minus";
static private String UNION_AND = "and";
static private String ZERO_TOKEN = "zero";
private AbstractProcessor processor = new CompositeBigProcessor(63);
@Override
public String getName(String value) {
boolean negative = false;
if (value.startsWith("-")) {
negative = true;
value = value.substring(1);
}
int decimals = value.indexOf(".");
String decimalValue = null;
if (0 <= decimals) {
decimalValue = value.substring(decimals + 1);
value = value.substring(0, decimals);
}
String name = processor.getName(value);
if ("".equals(name)) {
name = ZERO_TOKEN;
} else if (negative) {
name = MINUS.concat(SEPARATOR).concat(name);
}
if (!(null == decimalValue || "".equals(decimalValue))) {
String zeroDecimalValue = "";
for (int i = 0; i < decimalValue.length(); i++) {
zeroDecimalValue = zeroDecimalValue + "0";
}
if (decimalValue.equals(zeroDecimalValue)) {
name = name.concat(SEPARATOR).concat(UNION_AND).concat(SEPARATOR).concat(
"zero").concat(SEPARATOR).concat(
SCALE.getName(-decimalValue.length()));
} else {
name = name.concat(SEPARATOR).concat(UNION_AND).concat(SEPARATOR).concat(
processor.getName(decimalValue)).concat(SEPARATOR).concat(
SCALE.getName(-decimalValue.length()));
}
}
return name;
}
}
static public AbstractProcessor processor;
public static void main(String... args) {
processor = new DefaultProcessor();
long[] values = new long[] { 0, 4, 10, 12, 100, 108, 299, 1000, 1003, 2040, 45213, 100000,
100005, 100010, 202020, 202022, 999999, 1000000, 1000001, 10000000, 10000007,
99999999, Long.MAX_VALUE, Long.MIN_VALUE };
String[] strValues = new String[] { "0", "1.30", "0001.00", "3.141592" };
for (long val : values) {
System.out.println(val + " = " + processor.getName(val));
}
for (String strVal : strValues) {
System.out.println(strVal + " = " + processor.getName(strVal));
}
// generate a very big number...
StringBuilder bigNumber = new StringBuilder();
for (int d = 0; d < 66; d++) {
bigNumber.append((char) ((Math.random() * 10) + '0'));
}
bigNumber.append(".");
for (int d = 0; d < 26; d++) {
bigNumber.append((char) ((Math.random() * 10) + '0'));
}
System.out.println(bigNumber.toString() + " = " + processor.getName(bigNumber.toString()));
}
}
The output is
0 = zero
4 = four
10 = ten
12 = twelve
100 = one hundred
108 = one hundred eight
299 = two hundred ninety-nine
1000 = one thousand
1003 = one thousand three
2040 = two thousand fourty
45213 = fourty-five thousand two hundred thirteen
100000 = one hundred thousand
100005 = one hundred thousand five
100010 = one hundred thousand ten
202020 = two hundred two thousand twenty
202022 = two hundred two thousand twenty-two
999999 = nine hundred ninety-nine thousand nine hundred ninety-nine
1000000 = one million
1000001 = one million one
10000000 = ten million
10000007 = ten million seven
99999999 = ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine
9223372036854775807 = nine quintillion two hundred twenty-three quadrillion three hundred seventy-two trillion thirty-six billion eight hundred fifty-four million seven hundred seventy-five thousand eight hundred seven
-9223372036854775808 = minus nine quintillion two hundred twenty-three quadrillion three hundred seventy-two trillion thirty-six billion eight hundred fifty-four million seven hundred seventy-five thousand eight hundred eight
0.0 = zero and zero tenth
1.30 = one and thirty hundredth
0001.00 = one and zero hundredth
3.141592 = three and one hundred fourty-one thousand five hundred ninety-two millionth
354064188376576616844741830273568537829518115677552666352927559274.76892492652888527014418647 = three hundred fifty-four vigintillion sixty-four novemdecillion one hundred eighty-eight octodecillion three hundred seventy-six septendecillion five hundred seventy-six sexdecillion six hundred sixteen quindecillion eight hundred fourty-four quattuordecillion seven hundred fourty-one tredecillion eight hundred thirty duodecillion two hundred seventy-three undecillion five hundred sixty-eight decillion five hundred thirty-seven nonillion eight hundred twenty-nine octillion five hundred eighteen septillion one hundred fifteen sextillion six hundred seventy-seven quintillion five hundred fifty-two quadrillion six hundred sixty-six trillion three hundred fifty-two billion nine hundred twenty-seven million five hundred fifty-nine thousand two hundred seventy-four and seventy-six septillion eight hundred ninety-two sextillion four hundred ninety-two quintillion six hundred fifty-two quadrillion eight hundred eighty-eight trillion five hundred twenty-seven billion fourteen million four hundred eighteen thousand six hundred fourty-seven hundred-septillionth
import java.util.*;
public class NumberToWord {
public void numberToword(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.numberToword((n / 1000000000), " Hundred");
a.numberToword((n / 10000000) % 100, " crore");
a.numberToword(((n / 100000) % 100), " lakh");
a.numberToword(((n / 1000) % 100), " thousand");
a.numberToword(((n / 100) % 10), " hundred");
a.numberToword((n % 100), " ");
}
}
}
import java.util.Scanner;
public class StringToNum {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no: ");
int no=sc.nextInt();
int arrNum[]=new int[10];
int i=0;
while(no!=0)
{
arrNum[i]=no%10;
no=no/10;
i++;
}
int len=i;
int arrNum1[]=new int[len];
int j=0;
for(int k=len-1;k>=0;k--)
{
arrNum1[j]=arrNum[k];
j++;
}
StringToNum stn=new StringToNum();
String output="";
switch(len)
{
case 1:
{
output+=stn.strNum1(arrNum1[len-1]);
System.out.println("output="+output);
break;
}
case 2:
{
int no1=arrNum1[len-2]*10+arrNum1[len-1];
if(no1>=11 & no1<=19)
{
output=stn.strNum2(no1);
// output=output+" "+stn.strNum1(arrNum1[len-1]);
System.out.println("output="+output);
}
else
{
arrNum1[len-2]=arrNum1[len-2]*10;
output+=stn.strNum2(arrNum1[len-2]);
output=output+" "+stn.strNum1(arrNum1[len-1]);
System.out.println("output="+output);
}
break;
}
case 3:
{
output=stn.strNum1(arrNum1[len-3])+" hundred ";
int no1=arrNum1[len-2]*10+arrNum1[len-1];
if(no1>=11 & no1<=19)
{
output=stn.strNum2(no1);
}
else
{
arrNum1[len-2]=arrNum1[len-2]*10;
output+=stn.strNum2(arrNum1[len-2]);
output=output+" "+stn.strNum1(arrNum1[len-1]);
}
System.out.println("output="+output);
break;
}
case 4:
{
output=stn.strNum1(arrNum1[len-4])+" thousand ";
if(!stn.strNum1(arrNum1[len - 3]).equals(""))
{
output+=stn.strNum1(arrNum1[len-3])+" hundred ";
}
int no1=arrNum1[len-2]*10+arrNum1[len-1];
if(no1>=11 & no1<=19)
{
output=stn.strNum2(no1);
}
else
{
arrNum1[len-2]=arrNum1[len-2]*10;
output+=stn.strNum2(arrNum1[len-2]);
output=output+" "+stn.strNum1(arrNum1[len-1]);
}
System.out.println("output="+output);
break;
}
case 5:
{
int no1=arrNum1[len-5]*10+arrNum1[len-4];
if(no1>=11 & no1<=19)
{
output=stn.strNum2(no1)+" thousand ";
}
else
{
arrNum1[len-5]=arrNum1[len-5]*10;
output+=stn.strNum2(arrNum1[len-5]);
output=output+" "+stn.strNum1(arrNum1[len-4])+" thousand ";
}
if( !stn.strNum1(arrNum1[len - 3]).equals(""))
{
output+=stn.strNum1(arrNum1[len-3])+" hundred ";
}
no1 = arrNum1[len - 2] * 10 + arrNum1[len - 1];
if(no1>=11 & no1<=19)
{
output=stn.strNum2(no1);
}
else
{
arrNum1[len-2]=arrNum1[len-2]*10;
output+=stn.strNum2(arrNum1[len-2]);
output=output+" "+stn.strNum1(arrNum1[len-1]);
}
System.out.println("output="+output);
break;
}
case 6:
{
output+=stn.strNum1(arrNum1[len-6])+" million ";
int no1=arrNum1[len-5]*10+arrNum1[len-4];
if(no1>=11 & no1<=19)
{
output+=stn.strNum2(no1)+" thousand ";
}
else
{
arrNum1[len-5]=arrNum1[len-5]*10;
output+=stn.strNum2(arrNum1[len-5]);
output=output+" "+stn.strNum1(arrNum1[len-4])+" thousand ";
}
if( !stn.strNum1(arrNum1[len - 3]).equals(""))
{
output+=stn.strNum1(arrNum1[len-3])+" hundred ";
}
no1 = arrNum1[len - 2] * 10 + arrNum1[len - 1];
if(no1>=11 & no1<=19)
{
output=stn.strNum2(no1);
}
else
{
arrNum1[len-2]=arrNum1[len-2]*10;
output+=stn.strNum2(arrNum1[len-2]);
output=output+" "+stn.strNum1(arrNum1[len-1]);
}
System.out.println("output="+output);
break;
}
case 7:
{
int no1=arrNum1[len-7]*10+arrNum1[len-6];
if(no1>=11 & no1<=19)
{
output=stn.strNum2(no1)+" Milloin ";
}
else
{
arrNum1[len-7]=arrNum1[len-7]*10;
output+=stn.strNum2(arrNum1[len-7]);
output=output+" "+stn.strNum1(arrNum1[len-6])+" Million ";
}
no1=arrNum1[len-5]*10+arrNum1[len-4];
if(no1>=11 & no1<=19)
{
output=stn.strNum2(no1)+" Thousand ";
}
else
{
arrNum1[len-5]=arrNum1[len-5]*10;
output+=stn.strNum2(arrNum1[len-5]);
output=output+" "+stn.strNum1(arrNum1[len-4])+" Thousand ";
}
if( !stn.strNum1(arrNum1[len - 3]).equals(""))
{
output+=stn.strNum1(arrNum1[len-3])+" Hundred ";
}
no1 = arrNum1[len - 2] * 10 + arrNum1[len - 1];
if(no1>=11 & no1<=19)
{
output=stn.strNum2(no1);
}
else
{
arrNum1[len-2]=arrNum1[len-2]*10;
output+=stn.strNum2(arrNum1[len-2]);
output=output+" "+stn.strNum1(arrNum1[len-1]);
}
System.out.println("output="+output);
break;
}
}
}
String strNum1(int a)
{
String op="";
switch(a)
{
case 1:
{
op="one";
break;
}
case 2:
{
op="two";
break;
}
case 3:
{
op="three";
break;
}
case 4:
{
op="four";
break;
}
case 5:
{
op="five";
break;
}
case 6:
{
op="six";
break;
}
case 7:
{
op="seven";
break;
}
case 8:
{
op="eight";
break;
}
case 9:
{
op="nine";
break;
}
}
return op;
}
String strNum2(int a)
{
String op="";
switch(a)
{
case 10:
{
op="ten";
break;
}
case 20:
{
op="twenty";
break;
}
case 30:
{
op="thirty";
break;
}
case 40:
{
op="fourty";
break;
}
case 50:
{
op="fifty";
break;
}
case 60:
{
op="sixty";
break;
}
case 70:
{
op="seventy";
break;
}
case 80:
{
op="eighty";
break;
}
case 90:
{
op="ninty";
break;
}
case 11:
{
op="eleven";
break;
}
case 12:
{
op="twelve";
break;
}
case 13:
{
op="thirteen";
break;
}
case 14:
{
op="fourteen";
break;
}
case 15:
{
op="fifteen";
break;
}
case 16:
{
op="sixteen";
break;
}
case 17:
{
op="seventeen";
break;
}
case 18:
{
op="eighteen";
break;
}
case 19:
{
op="nineteen";
break;
}
}
return op;
}
}
I've developed a Java component to convert given number into words. All you've to do is - just copy the whole class from Java program to convert numbers to words and paste it in your project.
Just invoke it like below
Words w = Words.getInstance(1234567);
System.out.println(w.getNumberInWords());
My program supports up to 10 million. If you want, you can still extend this. Just below the example output
2345223 = Twenty Three Lakh Fourty Five Thousand Two Hundred Twenty Three
9999999 = Ninety Nine Lakh Ninety Nine Thousand Nine Hundred Ninety Nine
199 = One Hundred Ninety Nine
10 = Ten
Thanks
Santhosh