Java - Example to Round Float Value using Math.round() in Java.
IncludeHelp
22 August 2016
In this code snippet we will learn how to round float or double values using Math.round() in java programming?
Math.round() method is used to round float or double value to nearest integer, to know more about it , consider following example and see the output.
Java Code Snippet - Round Float or Double using Math.round()
//Java - Example to Round Float Value using Math.round() in Java.
import java.util.Scanner;
class RoundExample
{
public static void main(String args[]){
float number=0.0f;
System.out.print("Enter any float number: ");
Scanner SC=new Scanner(System.in);
number=SC.nextFloat();
System.out.println("number (w/o round): " + number);
System.out.println("number (with round): " + Math.round(number));
}
}
First Run
Enter any float number: 123.45678
number (w/o round): 123.45678
number (with round): 123
Second Run:
Enter any float number: 123.8909
number (w/o round): 123.8909
number (with round): 124