Computer Science Canada

Round to digits

Author:  KillCloud [ Wed Oct 11, 2006 6:03 pm ]
Post subject:  Round to digits

Please tell me how to round a number to digits, like round 1.345666 to 2 digits, thus, 1.35.

Author:  KillCloud [ Wed Oct 11, 2006 6:14 pm ]
Post subject: 

Ok i know that
code:
System.out.printf("%.2f", someNumber);

will round to 2 decimal place but i dont understand how it works
can someone explain it? thx

Author:  Andy [ Tue Oct 17, 2006 11:39 pm ]
Post subject: 

why not look it up on the [url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object...)]java[/url] api?

Author:  ericfourfour [ Thu Oct 19, 2006 10:44 pm ]
Post subject: 

Or you can make your own method. Here;s how it could look.

code:

int mult_by = 10 ^ decimal_places
int rounded = (int) (num * mult_by + 0.5)
double new_num = rounded / (mult_by * 1.0)


I'm pretty sure that should work.

Author:  zylum [ Fri Oct 20, 2006 12:00 am ]
Post subject: 

you could do a little bit better:

code:
double mult_by = Math.pow(10, decimal_places);
double rounded = Math.round (num * mult_by);
double new_num = rounded /mult_by;


*note this is untested but you get the point.. ei use the round() method rather than adding 0.5 and casting. also, if you are dealing with 2 ints and you are dividing and you want to keep decimal places, cast one of the ints to a double rather than multiply by 1.0

Author:  ericfourfour [ Fri Oct 20, 2006 4:31 pm ]
Post subject: 

another improvement would be:
code:

double mult_by = 1edecimal_places

That one can use negatives to you could round to negative decimals.
For example:
num = round (127, -2)
num would equal 100 (or 130 I can't think right now).

Author:  ericfourfour [ Sun Oct 22, 2006 2:35 am ]
Post subject: 

This one should work. I added 0.5 and casted to an integer because that way it isn't dependant on Math.round (using a separate rounding method for your rounding method doesn't make much sense). I also threw in a power method because the one in the Math class doesn't do negatives. This one has been tested and it works.

code:
public static double power (double base, int exponent)
{
    if (exponent < 0) return power (base, exponent + 1) / base;
    else if (exponent > 0) return power (base, exponent - 1) * base;
    return 1;
}
   
public static double round (double num, int decimal_places)
{
    double mult_by = power (10, decimal_places);
    double rounded = (int) (num * mult_by + 0.5);
    return rounded / mult_by;
}


code:
System.out.println (round (1251.345, -1));
Output: 1250.0

System.out.println (round (1251.345, 2));
Output: 1251.35

System.out.println (round (1251.345, 0));
Output: 1251.0

Author:  Ultrahex [ Sun Oct 22, 2006 10:15 pm ]
Post subject: 

Im Hoping You guys know that there is something called DECIMAL FORMATTING in java...... oh well lol

Author:  Aziz [ Mon Oct 23, 2006 9:15 am ]
Post subject: 

Guys, don't reinvent the wheel:

Math.round() will return a long type rounded to the nearest 1.
Math.pow(double x, double y) will return x^y

To round to 2 decimal places...

Java:
int x = 2.6666666666;
x = (int) (Math.round(x * 100) / 100);


Or, a simple function:

Java:
public long roundTo(double x, int places) {
  return Math.round(x * Math.pow(10, places)) / Math.pow(10, places'
}


That way, you can round to the nearest hundred (so 155 will be 200) by specifying -3 as the 'places'. Or something like that. No time to test it out, I'm at school.


: