1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/* Round Java float and double numbers using Math.round This java example shows how to round given float or double number using round method of Java Math class. */ public class RounFloatDoubleNumbersExample { public static void main(String[] args) { /* * To round float number, use * static int round(float f) method of Java Math class. * * It returns closest int number to the argument. * Internally, it adds 0.5 to the argument, takes floor value and casts * the result into int. * * i.e. result = (int) Math.floor( argument value + 0.5f ) */ //returns same value System.out.println(Math.round(10f)); // returns (int) Math.floor(10.6) = 10 System.out.println(Math.round(20.5f)); //returns (int) Math.floor(20.5 + 0.5) = 30 System.out.println(Math.round(20.5f)); //returns (int) Math.floor(-18.9) = 19 System.out.println(Math.round(-19.4f)); //returns (int) Math.floor(-23) = -23 System.out.println(Math.round(-23.5f)); /* * To round double numbers, use * static long round(double d) method of Java Math class. * It returns long. */ } } /* Output would be 10 21 21 -19 -23 */ |
Add Comment