Write a Java program to accept a float value of number and return a rounded float value

Write a Java program to accept a float value of number and return a rounded float value

Write a Java program to accept a float value of number and return a rounded float value

Program
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);
		}		
    }

}


Output:

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



More Questions


48 . Add all the digits of a given positive integer until the result has a single digit
49 . Write a Java program to accept a float value of number and return a rounded float value



For latest job updates join Telegram Channel: https://t.me/sateeshm

Apply For The Job

Apply For Job

Java Material

Python Material

PLSQL Material

.Net Material

Java Practice Programs

400 Java Programs