java concatenation help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: java concatenation help

  1. #1
    Join Date
    Apr 2006
    Posts
    20

    odd java rounding up problem...

    I'm not going to go into length about what the program does, as thats not the fix i want. The fix I want is that it only ever outputs is 3.0... it seems to round everything up and down, despite me never telling it to.

    Any ideas?

    Code:
    import java.util.*;
    import java.math.*;
    
    public class pi{
    	public static void main (String[]args){
    	
    	double pi=0;
    	double fraction=0;
    	String mathfunction;
    	int funccounter=0;
    	int denominator=0;
    	int loopcounter=0;
    	
    	pi=4;
    	denominator=1;
    	while (pi!=3.1415926)
    	{
    		if (loopcounter==0)
    		{
    			fraction=1-1/3;
    		}
    		else
    		{
    			fraction=1/denominator;
    		}
    		denominator=denominator+2;
    		if (funccounter % 2==0)
    		{
    			pi=pi-fraction;
    		}
    		else
    		{
    			pi=pi+fraction;
    		
    		}
    		System.out.println(pi);
    		if ((pi==3.14||pi==3.14159||pi==3.1415926))
    		{
    			System.out.println("Pi was equal to "+pi+" on the "+ loopcounter+ " st/nd/th loop");
    		}
    		loopcounter++;	
    		funccounter++;
    		
    		
    	}
    	}
    		
    }
    Last edited by ftr; 10-19-2006 at 11:08 PM. Reason: issue changed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2006, 04:16 PM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM
  3. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 06:32 PM
  4. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center