How can I round a number to the nearest ten with no if statements? For example, 98 to 100.
int num = 87;
double t;
double d = 1.0 * num; // d = 87.0
t = d/100;
System.out.println(t);
How can I round a number to the nearest ten with no if statements? For example, 98 to 100.
int num = 87;
double t;
double d = 1.0 * num; // d = 87.0
t = d/100;
System.out.println(t);
answer = ((num+5)/10)*10; // if num is int
where num
is int
and to have more idea, read this quesiton. How to round a number to n decimal places in Java.
Edit:
if num
is double
add typecasting to expression (long)((num+5)/10)
as suggested by @PeterLawrey