How to round to the nearest ten?

How can I round a number to the next ten without if statements? For example, from 98 to 100.

int num = 87; double t; double d = 1.0 * num; // d = 87.0 t = d/100; System.out.println(t); 
+5
source share
2 answers
 answer = ((num+5)/10)*10; // if num is int 

where num is an int , and to have more ideas, read this quesiton. How to round a number to n decimal places in Java .
Edit:
if num is double , add typecasting to the expression (long)((num+5)/10) as suggested by @PeterLawrey

+7
source

You can use Math.round(num/10.0) * 10 .

+6
source

Source: https://habr.com/ru/post/1257629/


All Articles