Rounding numbers in Java
java.lang.Math class has a static method
round() which accepts
double values as a parameter and returns respectively
For example, you have a number
3.1415 and want to round it to the nearest integer. Here's the Java code:
float pi = 3.1415f; int rounded = Math.round(pi);
Note that when you write
3.1415, you actually write it as
3.1415f. This is because Java treats any decimal constant you write into your code as type
double by default, and you must include a trailing '
f' if you wish the number to be treated as type
Feel free to post your questions or comments.