I want to write a java function for rounding the whole integer number into its nearest round number for example:
53 to 60
68 to 70
35 to 40
50 to 50
etc
I want to write a java function for rounding the whole integer number into its nearest round number for example:
53 to 60
68 to 70
35 to 40
50 to 50
etc
For your question nearest whole number would be for 53 -> 50 not 60
Any way for your question, it seems you need next multiple of 10. Very simple answer can be:
ans = (input%10) ? ((input/10)+1)*10 : input
Which simplifies like to,
To round a positive integer n
up to the nearest (positive) multiple m
:
(n + m - 1) / m
This avoids the unnecessary widening to double
that you'd get by using Math.ceil
.
e.g.
(n + 9) / 10
rounds up to the nearest 10.