Google
 
Web freesourcecode.blogspot.com
Subscribe to Free Java Source Code !!!

Wednesday, February 15, 2006

Java Source Code For Converting a Given "Number" into "Words" ...

This site has been moved to http://www.99applications.com
To download code/application click the following link 99applications.com/java_programs and you can also find many c and c plus plus common programs in C & C++ section




This program can genrally used by bank applications where you need to convert given "cheque" amount or "DD" amount in "words". or it can be used in any application where you want to display the number in word format. Hope this program works for you. :)


/***************************************************************/

Program to covert a given number in words Format
Author: Krishnakanth Soni
feedback at: sonikrishnakanth@gmail.com

usage: java Converter [list of numbers as arguments]
example: java Converter 1234 3456 4567 4321 9999

/**************************************************************/
Output Screen
Download API & Source Code for Converter here..

public class Converter
{
private double getPlace( String number ){
switch( number.length() ){
case 1:
return DefinePlace.UNITS;
case 2:
return DefinePlace.TENS;
case 3:
return DefinePlace.HUNDREDS;
case 4:
return DefinePlace.THOUSANDS;
case 5:
return DefinePlace.TENTHOUSANDS;
case 6:
return DefinePlace.LAKHS;
case 7:
return DefinePlace.TENLAKHS;
case 8:
return DefinePlace.CRORES;
case 9:
return DefinePlace.TENCRORES;
}//switch
return 0.0;
}// getPlace

private String getWord( int number ){
switch( number ){
case 1:
return "One";
case 2:
return "Two";
case 3:
return "Three";
case 4:
return "Four";
case 5:
return "Five";
case 6:
return "Six";
case 7:
return "Seven";
case 8:
return "Eight";
case 9:
return "Nine";
case 0:
return "Zero";
case 10:
return "Ten";
case 11:
return "Eleven";
case 12:
return "Tweleve";
case 13:
return "Thirteen";
case 14:
return "Forteen";
case 15:
return "Fifteen";
case 16:
return "Sixteen";
case 17:
return "Seventeen";
case 18:
return "Eighteen";
case 19:
return "Ninteen";
case 20:
return "Twenty";
case 30:
return "Thirty";
case 40:
return "Forty";
case 50:
return "Fifty";
case 60:
return "Sixty";
case 70:
return "Seventy";
case 80:
return "Eighty";
case 90:
return "Ninty";
case 100:
return "Hundred";
} //switch
return "";
} //getWord

private String cleanNumber( String number ){
String cleanedNumber = "";

cleanedNumber = number.replace( '.', ' ' ).replaceAll( " ", "" );
cleanedNumber = cleanedNumber.replace( ',', ' ' ).replaceAll( " ", "" );
if( cleanedNumber.startsWith( "0" ) )
cleanedNumber = cleanedNumber.replaceFirst( "0", "" );

return cleanedNumber;
} //cleanNumber

public String convertNumber( String number ){
number = cleanNumber( number );
double num = 0.0;
try{
num = Double.parseDouble( number );
}catch( Exception e ){
return "Invalid Number Sent to Convert";
} //catch

String returnValue = "";
while( num > 0 ){
number = "" + (int)num;
double place = getPlace(number);
if( place == DefinePlace.TENS || place == DefinePlace.TENTHOUSANDS || place == DefinePlace.TENLAKHS || place == DefinePlace.TENCRORES ){
int subNum = Integer.parseInt( number.charAt(0) + "" + number.charAt(1) );

if( subNum >= 21 && (subNum%10) != 0 ){
returnValue += getWord( Integer.parseInt( "" + number.charAt(0) ) * 10 ) + " " + getWord( subNum%10 ) ;
} //if
else{
returnValue += getWord(subNum);
}//else

if( place == DefinePlace.TENS ){
num = 0;
}//if
else if( place == DefinePlace.TENTHOUSANDS ){
num -= subNum * DefinePlace.THOUSANDS;
returnValue += " Thousands ";
}//if
else if( place == DefinePlace.TENLAKHS ){
num -= subNum * DefinePlace.LAKHS;
returnValue += " Lakhs ";
}//if
else if( place == DefinePlace.TENCRORES ){
num -= subNum * DefinePlace.CRORES;
returnValue += " Crores ";
}//if
}//if
else{
int subNum = Integer.parseInt( "" + number.charAt(0) );

returnValue += getWord( subNum );
if( place == DefinePlace.UNITS ){
num = 0;
}//if
else if( place == DefinePlace.HUNDREDS ){
num -= subNum * DefinePlace.HUNDREDS;
returnValue += " Hundred ";
}//if
else if( place == DefinePlace.THOUSANDS ){
num -= subNum * DefinePlace.THOUSANDS;
returnValue += " Thousand ";
}//if
else if( place == DefinePlace.LAKHS ){
num -= subNum * DefinePlace.LAKHS;
returnValue += " Lakh ";
}//if
else if( place == DefinePlace.CRORES ){
num -= subNum * DefinePlace.CRORES;
returnValue += " Crore ";
}//if
}//else
}//while
return returnValue;
}//convert number

public static void main( String args[] ){
Converter cv = new Converter();

if( args.length >= 1 )
{
for( int i=0; i<args.length; i++ )
System.out.println( "Given Number : " + args[i] + "\nConverted: " + cv.convertNumber(args[i]) + "\n\n" );
System.exit(0);
}

System.out.println( "Given Number : 999999999\nConverted: " + cv.convertNumber("999999999") + "\n\n" );
}//main
} //class

class DefinePlace{
public static final double UNITS = 1;
public static final double TENS = 10 * UNITS;
public static final double HUNDREDS = 10 * TENS;
public static final double THOUSANDS = 10 * HUNDREDS;
public static final double TENTHOUSANDS = 10 * THOUSANDS;
public static final double LAKHS = 10 * TENTHOUSANDS;
public static final double TENLAKHS = 10 * LAKHS;
public static final double CRORES = 10 * TENLAKHS;
public static final double TENCRORES = 10 * CRORES;
} //class


Home

7 Comments:

At 1:49 AM, Anonymous Anonymous said...

with out main or applet how can u expect one to execute ur program.

 
At 11:11 PM, Anonymous Anonymous said...

hi ,
dats a nice piece of code.
There is a small request i need to use it my office n they don' allow to install win rar can u please upload it .zip.
thanks in advance, or if possible can u mail me this on my id sachin.kumar01@gmail.com

 
At 4:44 AM, Anonymous Anonymous said...

Write a program in Sun Java to convert a given number to words. The number must be input by the user. It may be as long as 4 digits. For example:
345 - Three hundred and forty five
1037 - One thousand three hundred and thirty seven
Make use of user defined functions and switch statement wherever neccessary.

Please do give a variable list, as in variable name, type, and its function. I do not know what is meant by 'program listing' but if you can, please give that too!

We arent allowed to use strings and arrays.

It's really really really urgent!! It may take you some time, but I'd be deeply, truly, sincerely gratified to you!!!!!! It's very very important!!!! Please help!!!!

Please email it on cutencrazylittlegirl@yahoo.co.in!!
Thank you!

 
At 1:11 AM, Anonymous Anonymous said...

it was helpful indeed, but the catch is ...my class has been given the same program albeit using arrays and not switch case, i am just lost with the logic ...help me out, if you will please. 2 days for submission.

 
At 1:14 AM, Anonymous Anonymous said...

oops! my email is chandan-p14@indiatimes.com
waiting for obligations!!!

 
At 1:31 PM, Anonymous Anonymous said...

String[] word0_9 = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

private String convertToWord(int number){

if(number>=0 && number<=9 )
{
return word0_9[number];
}
int temp=number;

String words="";

if(number >=11 && number <=19)
{
return word11_19[number-11];
}

number=number/10;
if(number!=0)

 
At 7:50 AM, Blogger Lov KiemTheng said...

if i would to read a float number in words, how can i do ? if someone can help me about it please let me know by email LKT@msdncambodia.net

 

Post a Comment

<< Home