-3

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

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Snehal
  • 9
  • 2

2 Answers2

3

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,

  1. input = 52
  2. (input/10)+1 = 6
  3. 6*10 = 60
Bhavesh
  • 882
  • 2
  • 9
  • 18
0

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243