Skip to content

Instantly share code, notes, and snippets.

@ellbur
Created December 15, 2011 20:43
Show Gist options
  • Select an option

  • Save ellbur/1482786 to your computer and use it in GitHub Desktop.

Select an option

Save ellbur/1482786 to your computer and use it in GitHub Desktop.
Rounding
public class Round {
public static void main(String[] args) {
System.out.println(round(3.124, 2));
}
static double round(double x, int n) {
double y = x * tenToN(n);
y = Math.round(y);
return y / tenToN(n);
}
static double tenToN(int n) {
if (n > 0) {
return 10 * tenToN(n-1);
}
else {
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment