import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input a float number: "); float x = in.nextFloat(); System.out.printf("\n"); float f_num = (float)Math.floor(x); float c_num = (float)Math.ceil(x); if ((x - f_num) > (c_num - x)) { System.out.printf("The rounded value of %f is: %.2f",x, c_num); } else if ((c_num - x) > (x - f_num)) { System.out.printf("The rounded value of %f is: %.2f",x, f_num); } else { System.out.printf("The rounded value of %f is: %.2f",x, c_num); } } }
Input a float number: 12.53
The rounded value of 12.530000 is: 13.00
Input a float number: 24.578
The rounded value of 24.577999 is: 25.00