Skip to content

Instantly share code, notes, and snippets.

@DCCoder90
Created January 18, 2018 04:48
Show Gist options
  • Save DCCoder90/df7eae7fea44d90801fa406f1433a221 to your computer and use it in GitHub Desktop.
Save DCCoder90/df7eae7fea44d90801fa406f1433a221 to your computer and use it in GitHub Desktop.
A few extension methods for rounding
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