This site requires JavaScript, please enable it in your browser!
Greenfoot back
Entity1037
Entity1037 wrote ...

2014/2/11

Method giving me incorrect numbers for some reason.

Entity1037 Entity1037

2014/2/11

#
I've made this code that, in theory (I'm pretty sure), should round any double to as many decimals as you want. However, some of the calculations are giving me some wierd numbers. Does anyone have any clue as to why?
    public static void main(String[] args) {

        double roundedNumber=76.9864590256;  //Some test decimal I randomly punched in
    	
        for (double i = 0.0000000005; i<0.01; i*=10){
            System.out.println("\n"+(roundedNumber+i)+"\n"+roundedNumber+((i*10)/5));
        	if (roundedNumber+i>=roundedNumber+((i*10)/5)){
	    roundedNumber=roundedNumber-(roundedNumber%(i*2))+((i*10)/5);
	    System.out.println("Round Up: "+roundedNumber);
        	}else{
            	    roundedNumber=roundedNumber-(roundedNumber%(i*2));
            	    System.out.println("Round Down: "+roundedNumber);
                }
        }
        System.out.println("\n"+roundedNumber);
    }
The code's spacing got a little uneven for some reason.
Entity1037 Entity1037

2014/2/11

#
    public static void main(String[] args) {

    	double roundedNumber=76.9864590256;  //Some test decimal I randomly punched in
    	
   	 	for (double i = 0.0000000005; i<0.01; i*=10){
   	 		System.out.println("\n"+(roundedNumber+i)+"\n"+roundedNumber+((i*10)/5));
        	if (roundedNumber+i>=roundedNumber+((i*10)/5)){
	            roundedNumber=roundedNumber-(roundedNumber%(i*2))+((i*10)/5);
	            System.out.println("Round Up: "+roundedNumber);
        	}else{
            	roundedNumber=roundedNumber-(roundedNumber%(i*2));
            	System.out.println("Round Down: "+roundedNumber);
        	}
    	}
    	System.out.println("\n"+roundedNumber);
    }
That might be better...
erdelf erdelf

2014/2/11

#
I am not quite sure that i understand ur code, but why dont just use Math.round() `?
kiarocks kiarocks

2014/2/11

#
I can't remember why, but because doubles are more precise, sometimes you get 1.00000000001 instead of 1.
Entity1037 Entity1037

2014/2/12

#
The goal of this is to round to as many decimals as I want. So I could just set i<0.01 to something like i<0.001 and then it will round to 3 decimal places instead of 2. And actually, the Math.round() method is inaccurate, as it only looks at the first decimal place, and rounds up to a whole number. I fixed my method. I apparently did some wrong calculations. This one should work (although when I add a 0.00005 for instance, it will sometimes just stick a bunch of 9s at the end of the number instead, which messes up the whole method).
import java.util.Scanner;
public class Round {

    public static void main(String[] args) {
    	Scanner user = new Scanner(System.in);
        System.out.print("Enter the number to round: ");
        double roundedNumber=user.nextDouble();
        System.out.print("Enter the the number of decimal places you wish to round to: ");
        double n=1;
        double d=user.nextInt();
        for (int p=0; p<d; p++){n/=10;}
        System.out.println("Math.round(): "+Math.round(roundedNumber));
        for (double i=0.00000000000005; i<n; i*=10){
            if (roundedNumber+i>=roundedNumber+((i*10)/5)-(roundedNumber%(i*2))){  
            	roundedNumber=roundedNumber-(roundedNumber%(i*2))+((i*10)/5);  
            	//System.out.println("Round Up: "+roundedNumber);  
            }else{  
                roundedNumber=roundedNumber-(roundedNumber%(i*2));  
                //System.out.println("Round Down: "+roundedNumber);  
            }  
        }  
        System.out.println("\n"+roundedNumber);
    }
}
danpost danpost

2014/2/12

#
You could just use this:
System.out.println(""+number);
for (int i=9; i>=0; i--)
{
    number = ((double)Math.round(number*Math.pow(10, i)))/Math.pow(10, i);
    System.out.println(""+number);
}
'i' being the number of decimal digits to round to.
Entity1037 Entity1037

2014/2/12

#
My goal was to figure out how to make a method that rounds to x decimals on my own, so doing that would have been kind of cheating. Thanks anyway.
You need to login to post a reply.