'How do I round to the nearest ten? [duplicate]
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);
Solution 1:[1]
You can use Math.round(num/10.0) * 10.
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Community |
