Skip to content

Instantly share code, notes, and snippets.

@jliu9
Forked from aslakhellesoy/rounding.java
Created April 9, 2019 00:32
Show Gist options
  • Save jliu9/3239ce7eed22f6c5b48347b961831b80 to your computer and use it in GitHub Desktop.
Save jliu9/3239ce7eed22f6c5b48347b961831b80 to your computer and use it in GitHub Desktop.
Rounding up and down to nearest multiple
/** round n down to nearest multiple of m */
long roundDown(long n, long m) {
return n >= 0 ? (n / m) * m : ((n - m + 1) / m) * m;
}
/** round n up to nearest multiple of m */
long roundUp(long n, long m) {
return n >= 0 ? ((n + m - 1) / m) * m : (n / m) * m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment