-3

How I make approximation for a large 5, numbers C# code example

62     I want to get 65 
63     get   65
71.15  get  75
Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
  • 2
    would 77 be 80? Would 66 be 70? – Charles May Apr 09 '19 at 19:51
  • Obtain the digits first. Use ```n % 10``` to obtain the first (right-moist) digit. The, ```n/10``` will return the other digits plus a reminder. Eliminate the reminder with the equivalent in C# for ```Math.floor``` – acarlstein Apr 09 '19 at 19:54
  • 1
    Possible duplicate of [Round a number to the next HIGHEST 10](https://stackoverflow.com/questions/8679697/round-a-number-to-the-next-highest-10) – devNull Apr 09 '19 at 20:00

3 Answers3

2

Try something like this:

Math.Round(x - x % 5) + 5

dotnetfiddle

Johnny
  • 8,939
  • 2
  • 28
  • 33
1
  • Divide by 5.
  • Find the ceiling.
  • Multiply by 5.
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

One solution:

double num = 64.65;
double nextMultiplyOf5 = (int)(num + 4) / 5 * 5;
Amir Molaei
  • 3,700
  • 1
  • 17
  • 20