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
Visit http://freesourcecode.blogspot.com for Thambola, Housie, Love Calculator, Friendship Calculator, FLAMES, Animations, Digital Number, Sudoku.java - Java Programmes.
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
/**************************************************************/
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
No comments:
Post a Comment