Solved

# How do I round a number (Simple Java Calculator)

Posted on 2004-05-03

Hello

I have built a calculator that is pretty much complete but for the inability to round numbers

what I want to do is say input a number x (double) press the "round" key, enter another number(y double and converted to int) and that number will be rounded to that number (y).

Example

9.4524 is entered (x)

Press Round Button

2 is entered (y)

result = 9.45 (9.4524 to y decimal places)

I have been trying to implement this but with out success

Here is some "example Code"

public double Rounded(Double d, int i){

BigDecimal rnd = new BigDecimal(d); /* the constructor is the only way I could find to convert */

rnd.setScale(i); /* a double to BigDecimal format */

return rnd.doubleValue();

}

Cheers

Murray S

PS I have little in the way of knowlege re: BigDecimal so quick tutorial would be good