ADVERTISEMENT
ADVERTISEMENT

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

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.