
-----------------------------------
KillCloud
Wed Oct 11, 2006 6:03 pm

Round to digits
-----------------------------------
Please tell me how to round a number to digits, like round 1.345666 to 2 digits, thus, 1.35.

-----------------------------------
KillCloud
Wed Oct 11, 2006 6:14 pm


-----------------------------------
Ok i know that 
System.out.printf("%.2f", someNumber);
will round to 2 decimal place but i dont understand how it works
can someone explain it? thx

-----------------------------------
Andy
Tue Oct 17, 2006 11:39 pm


-----------------------------------
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 api?

-----------------------------------
ericfourfour
Thu Oct 19, 2006 10:44 pm


-----------------------------------
Or you can make your own method. Here;s how it could look.


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.

-----------------------------------
zylum
Fri Oct 20, 2006 12:00 am


-----------------------------------
you could do a little bit better:

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

-----------------------------------
ericfourfour
Fri Oct 20, 2006 4:31 pm


-----------------------------------
another improvement would be:

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).

-----------------------------------
ericfourfour
Sun Oct 22, 2006 2:35 am


-----------------------------------
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.

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;
}


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


-----------------------------------
Ultrahex
Sun Oct 22, 2006 10:15 pm


-----------------------------------
Im Hoping You guys know that there is something called DECIMAL FORMATTING in java...... oh well lol

-----------------------------------
Aziz
Mon Oct 23, 2006 9:15 am


-----------------------------------
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...

int x = 2.6666666666;
x = (int) (Math.round(x * 100) / 100);

Or, a simple function:

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.
