AllWillB1
AllWillB1

Reputation: 13

Java rounding double to arbitrary number (9)

I'm trying to make a calculator that will add two numbers and double them then take the doubled value and round it UP to the nearest nine. i have been unable to figure out how to make it always round up to the nearest 9. so 13.33 should become 19.00 or 19 or can even read 19.99 if it HAD too i can just disregard that pennies.

Upvotes: 1

Views: 165

Answers (2)

Buddy
Buddy

Reputation: 11028

I think this will do it:

 x = (int)Math.ceil((n1 + n2) * 2)
 x = x + (9 - (x % 10))

Upvotes: 1

Jase Pellerin
Jase Pellerin

Reputation: 397

Try this:

double x = 4.99;
double y = 6.99;
double z = x + y;
float xFloat;

z = z * 2;
xFloat = (float)Math.ceil(z);
xFloat = xFloat + (9 - xFloat % 10);
System.out.print(xFloat);

Upvotes: 0

Related Questions