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);
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
num
int
double
(long)((num+5)/10)
You can use Math.round(num/10.0) * 10 .
Math.round(num/10.0) * 10
Source: https://habr.com/ru/post/1257629/More articles:What are the various bars (status bar, action bar, navigation bar, toolbar, etc.) available in android? - androidHow to get username from Django Rest Framework JWT token - djangoHow to prevent googleVis tag reduction? - rApache Hama on Amazon Elastic MapReduce - apachejava restful services, how dependence on war on war - javaThe argument type 'Range ' does not match the expected type 'Sequence' Swift3 - arraysthere is no factory type service available in ProjectScopeServices gradle synchronization problem - androidconda stuck in proceed ([y] / n)? when updating packages in ipython console - pythonHow to add a custom element to an Android theme ad? - androidHow to transfer data from SqLite to Firebase real-time database in android? - androidAll Articles