Sandeep & Vicky’s Archive » 2009 » June

Archive for June, 2009

5
Jun

java code to convert double data type into int

   Posted by: admin    in java

It is a very simple code and  very interesting. It is a two step process: First convert the double to string and then convert the string to int. Here is the code for the same:

CODE

double d=198.17;

int i=0;

String stotat= “”+d; // Convert double to string

i=Integer.parseInt(stotal.substring(0,stotal.indexOf(“.”))); // convert string to int.

System.out.println(“Integer=”+i);

//  thats is….put it in a class to see it working

//jai Convergence (www.convergenceservices.in)

4
Jun

java code to convert a number to its word form

   Posted by: admin    in java

Hey guys if you want a code which will convert a number to its word format then here is the code you are looking for. This one is normally used in a lot of banks and places where there is a hell lot of economic transactions. For example a billing software would obviously require this. It does the foll

929 = Nine hundered twenty nine

So, here is the code

public class NumWord {
String string;
String a[]={“”,
“one”,
“two”,
“three”,
“four”,
“five”,
“six”,
“seven”,
“eight”,
“nine”,
};

String b[]={
“hundred”,
“thousand”,
“lakh”,
“crore”

};

String c[]={“ten”,
“eleven”,
“twelve”,
“thirteen”,
“fourteen”,
“fifteen”,
“sixteen”,
“seventeen”,
“eighteen”,
“ninteen”,
};

String d[]={

“twenty”,
“thirty”,
“fourty”,
“fifty”,
“sixty”,
“seventy”,
“eighty”,
“ninty”
};

public String convertNumToWord(int number){

int c=1;
int rm ;
string=”";
while ( number != 0 )
{
switch ( c )
{
case 1 :
rm = number % 100 ;
pass ( rm ) ;
if( number > 100 && number % 100 != 0 )
{
display ( “and ” ) ;
}
number /= 100 ;

break ;

case 2 :
rm = number % 10 ;
if ( rm != 0 )
{
display ( ” ” ) ;
display ( b[0] ) ;
display ( ” ” ) ;
pass ( rm ) ;
}
number /= 10 ;
break ;

case 3 :
rm = number % 100 ;
if ( rm != 0 )
{
display ( ” ” ) ;
display ( b[1] ) ;
display ( ” ” ) ;
pass ( rm ) ;
}
number /= 100 ;
break ;

case 4 :
rm = number % 100 ;
if ( rm != 0 )
{
display ( ” ” ) ;
display ( b[2] ) ;
display ( ” ” ) ;
pass ( rm ) ;
}
number /= 100 ;
break ;

case 5 :
rm = number % 100 ;
if ( rm != 0 )
{
display ( ” ” ) ;
display ( b[3] ) ;
display ( ” ” ) ;
pass ( rm ) ;
}
number /= 100 ;
break ;

}
c++ ;
}

return string;
}

public void pass(int number)
{
int rm, q ;
if ( number < 10 )
{
display ( a[number] ) ;
}

if ( number > 9 && number < 20 )
{
display ( c[number-10] ) ;
}

if ( number > 19 )
{
rm = number % 10 ;
if ( rm == 0 )
{
q = number / 10 ;
display ( d[q-2] ) ;
}
else
{
q = number / 10 ;
display ( a[rm] ) ;
display ( ” ” ) ;
display ( d[q-2] ) ;
}
}
}

public void display(String s)
{
String t ;
t= string ;
string= s ;
string+= t ;
}

public static void main(String args[]){

NumWord num=new NumWord();
System.out.println(“num.convertNumToWord(0)”+num.convertNumToWord(Integer.parseInt(args[0])));

}

}

OUTPUT :

Whatever number you pass as command line argument would be converted to its word format.

Related Posts with Thumbnails