简体   繁体   中英

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.

I think this will do it:

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM