- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Round float and double numbers in Java
In order to round float and double numbers in Java, we use the java.lang.Math.round() method. The method accepts either double or float values and returns an integer value. It returns the closest integer to number. This is computed by adding ½ to the number and then flooring it.
Declaration - The java.lang.Math.round() method is declared as follows −
public static int round(float a) public static int round(double a)
where a is the value to be rounded.
Let us see a program where float and double numbers are rounded in Java.
Example
import java.lang.Math; public class Example { public static void main(String[] args) { // declaring and initializing some double and values double x = 25.5; float y = 8.1f; // printing the rounded values System.out.println("The round-off of "+ x + " is "+ Math.round(x)); System.out.println("The round-off of "+ y + " is "+ Math.round(y)); } }
Output
The round-off of 25.5 is 26 The round-off of 8.1 is 8
- Related Articles
- Float and Double in C
- Comparison of double and float primitive types in Java\n
- Difference Between Float and Double
- Modulus of two float or double numbers using C
- Difference between float and double in Arduino
- Get the absolute value of float, int, double and long in Java
- How to compare float and double in C++?
- Difference between float and double in C/C++
- Java Program to round a double passed to BigDecimal
- Comparison of double and float primitive types in C#
- When can a double-type be preferred over float-type in Java?
- How to convert amount to float and round correctly in PHP?
- How do you round up a float number in Python?
- What is the difference between a float, double and a decimal in C#?
- Checking if a double (or float) is NaN in C++

Advertisements