Created
December 15, 2011 20:43
-
-
Save ellbur/1482786 to your computer and use it in GitHub Desktop.
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 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