Advertisement
PsHegger

Round to specific number

Aug 5th, 2013
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.34 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int round_to(int original, int to)
  4. {
  5.     int mod = original % to;
  6.     float to_half = to / 2.0f;
  7.    
  8.     if (mod < to_half)
  9.     {
  10.         return original - mod;
  11.     }
  12.     else
  13.     {
  14.         return original + (to - mod);
  15.     }
  16. }
  17.  
  18. int main()
  19. {
  20.     int i;
  21.    
  22.     for (i = 0; i < 30; i++)
  23.     {
  24.         printf("%d -> %d\n", i, round_to(i, 5));
  25.     }
  26.    
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement