BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.round() Method

By Chaitanya Singh | Filed Under: java

Java Math.round() method returns closest number to the passed argument. For example, Math.round(15.75) would return 16. In this tutorial, we will discuss the round() method with examples.

public class JavaExample
{
  public static void main(String[] args)
  {
    double d = 15.75;
    float f = -7.6f;
    // closest long value when rounding off double
    System.out.println(Math.round(d));

    //closest int value when rounding off float
    System.out.println(Math.round(f));
  }
}

Output:

16
-8

Syntax of Math.round() Method

Math.round(18.51) //returns 19

round() Description

There are two variations of round() method:
public static int round(float num): Returns closest int number.
public static long round(double num): Returns closest long number.

round() Parameters

It takes a single parameter:

  • num: The number that is passed as argument.

round() Return Values

  • Returns nearest int value if passed argument is float and nearest long value if passed argument is double.
  • If passed argument is NaN (Not a number) then it returns 0 (zero).
  • If passed float argument is negative infinity then Integer.MIN_VALUE is returned. If passed float argument is positive infinity then Integer.MAX_VALUE is returned.
  • If passed double argument is negative infinity then Long.MIN_VALUE is returned. If passed float argument is positive infinity then Long.MAX_VALUE is returned.

Example 1: Math.round() with float values

public class JavaExample
{
  public static void main(String[] args)
  {
    float f1 = -7.6f, f2 = 19.99f;
    System.out.println(Math.round(f1));
    System.out.println(Math.round(f2));
  }
}

Output:

Java Math.round() Example Output_1

Example 2: Math.round() with double values

public class JavaExample
{
  public static void main(String[] args)
  {
    double d1 = -18.85, d2 = 6.49;
    System.out.println(Math.round(d1));
    System.out.println(Math.round(d2));
  }
}

Output:

Java Math.round() Example Output_2

Example 3: Round off Positive and Negative Infinity

public class JavaExample
{
  public static void main(String[] args)
  {
    double d1 = Double.NEGATIVE_INFINITY;
    double d2 = 10.0/0; //positive infinity
    System.out.println(Math.round(d1)); //Long.MIN_VALUE
    System.out.println(Math.round(d2)); //Long.MAX_VALUE
  }
}

Output:

Java Math.round() Example Output_3

Example 4: Method round() with NaN

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = 0.0/0; //NaN (Not a Number)
    System.out.println("NaN rounded to: "+Math.round(num));
  }
}

Output:

Java Math.round() Example Output_4

Recommended Posts

  • Java Math.min()
  • Java Math.max()
  • Java Math.abs()
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap