Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Converting numbers to words

  Asked By: Clifford    Date: May 06    Category: Java    Views: 2305
  

This is a program that converts a number to string form. You can copy my
ConvertNumberToString() function which converts the number and returns the
string.

package NumberConvert;
import java.io.*;

public class NumberConvert {
/*
* This function converts numbers into numbers in string form.
* @param num This is the number to be converted into String form
* @returns The number in String form. i.e., a String
* @author Hariprasad Govardhanam
*/
public String ConvertNumberToString(int num) {
//Used to measure the length of the String
String numstring=Integer.toString(num);
//Used to store the result of the String and this is the variable
returned
String result="";
//Used to store the length of the number in digits
int length=numstring.length();
//This is used to store the position of a digit on which operation is
performed
int count=length;

System.out.println("Got a number of "+length+" digits");

//Start of a loop
for(int i=1;i<=length;i++) {
int value;

if(count<=4) // if a four or less digit number is being processed
value=(int)num/((int)Math.pow(10,length-i));
else { // if more than four digit number is given then executed
value=(int)num/1000;
result=result+ConvertNumberToString(value)+"Thousands, ";
count=3;
i=length-3;
num=num-(value*1000);
continue;
}

if(count==2) { //if count is 2 then the number is at Tens place
switch(value) {
case 1:
if(num==11) {
result=result+"Eleven";
}else if(num==12) {
result=result+"Twelve";
}else if(num==13) {
result=result+"Thirteen";
}else if(num==14) {
result=result+"Fourteen";
}else if(num==15) {
result=result+"Fifteen";
}else if(num==16) {
result=result+"Sixteen";
}else if(num==17) {
result=result+"Seventeen";
}else if(num==18) {
result=result+"Eighteen";
}else if(num==19) {
result=result+"Nineteen";
}
break;
case 2:
result=result+"Twenty";
break;
case 3:
result=result+"Thirty";
break;
case 4:
result=result+"Forty";
break;
case 5:
result=result+"Fifty";
break;
case 6:
result=result+"Sixty";
break;
case 7:
result=result+"Seventy";
break;
case 8:
result=result+"Eighty";
break;
case 9:
result=result+"Ninty";
break;
default:
break;
}
}
else {
switch(value) {
case 1:
result=result+"One";
break;
case 2:
result=result+"Two";
break;
case 3:
result=result+"Three";
break;
case 4:
result=result+"Four";
break;
case 5:
result=result+"Five";
break;
case 6:
result=result+"Six";
break;
case 7:
result=result+"Seven";
break;
case 8:
result=result+"Eight";
break;
case 9:
result=result+"Nine";
break;
case 10:
result=result+"Ten";
break;
default:
break;
}

if(count==5) {

}
else if(count==4) { // if a fourth postion number is processed
then add
result=result+" Thousand";
}
else if(count==3) { // if a third position number is processed
then add
result=result+" Hundred and";
}
}

// add a space after every number
result=result+" ";
// decrement the position of the digit to be processed
count=count-1;
// deduct the value of the num so that the processed digit goes out
num=num-(value*(int)Math.pow(10,length-i));
}
// return the number in string form
return result;
}
public static void main(String[] args) {
int num=148305;
/*System.out.println("Enter a number");
try {
num=System.in.read();
}catch(IOException e) {
e.printStackTrace();
}*/
NumberConvert numcon=new NumberConvert();
String result=numcon.ConvertNumberToString(num);
System.out.println(result);
}
}

Share: 

 

1 Answer Found

 
Didn't find what you were looking for? Find more on Converting numbers to words Or get search suggestion and latest updates.




Tagged: