Go to Top

Sunday 2 October 2011

Utilities 10 : Approximate

Hope you are all enjoying Durga Puja 2011. Here's a quick tool that will approximate any decimal value to two decimal places.

Java Program :

import java.io.*;
class approximation
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     String a,b="",e="";
     int i,c=0,d=0;
     double input,app=0,num=0;
     char k;
     System.out.print("Enter the number : ");
     input=Double.parseDouble(br.readLine());
     a=Double.toString(input);
     for(i=0;i<=a.length()-1;i++)
     {
         k=a.charAt(i);
         if(k=='.')
         {
         c=i+1;
         d=i-1;
        }
        }
        for(i=c;i<=c+2;i++)
        b=b+a.charAt(i);
        for(i=0;i<=d;i++)
        e=e+a.charAt(i);
        app=Integer.parseInt(e);
        num=Integer.parseInt(b);
        if(num%10>=5)
        {
        num=num-(num%10);
        num=num+10;
        }
        else
        num=num-(num%10);
        num=num/1000;
        app=app+num;
        System.out.print("\nThe approx. value is : "+app);
    }
}
         
         
       
   

No comments:

Post a Comment

ShareThis