Created
January 18, 2018 04:48
-
-
Save DCCoder90/df7eae7fea44d90801fa406f1433a221 to your computer and use it in GitHub Desktop.
A few extension methods for rounding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Extensions{ | |
/** | |
* Rounds the supplied number to the nearest multiple of the parameter | |
* @param this int The number to round | |
* @param int The multiple to use | |
* @return int | |
* @static | |
* @extension | |
*/ | |
public static int MultipleRound(this int number,int multiple){ | |
int rem = number % multiple; | |
if(rem == 0){ | |
return number; | |
}else{ | |
while(rem!=0){ | |
rem=number%multiple; | |
if(rem>(multiple/2)){ | |
number+=rem; | |
}else{ | |
number-=rem; | |
} | |
} | |
return number; | |
} | |
} | |
/** | |
* Rounds the supplied number to the nearest multiple of the parameter | |
* @param this float The number to round | |
* @param float The multiple to use | |
* @return int | |
* @static | |
* @extension | |
*/ | |
public static int MultipleRound(this float number,float multiple){ | |
float rem = number % multiple; | |
if(rem == 0){ | |
return (int)number; | |
}else{ | |
while(rem!=0){ | |
rem=(int)number%multiple; | |
if(rem>(multiple/2)){ | |
number+=rem; | |
}else{ | |
number-=rem; | |
} | |
} | |
return (int)number; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment