Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Java rint Function

by suresh

The Java rint Function is one of the Java Math Library functions, which is to round the specified expression or an individual number to the nearest mathematical integer. In this article, we will show how to use Java Math.rint function with example.

Java rint Function syntax

The basic syntax of the Math.rint Function in Java Programming language is as shown below.

static double rint(double number); //Return Type is Double

// In order to use in program: 
Math.rint(double number);

Number: It can be a double value or a valid numerical expression.

  • If the number argument is a positive or negative number, Math.rint function will return the nearest value.
  • If the number argument is not a number (NaN) or an infinity or positive zero or negative zero, the Java Math.rint function will return the argument value as a result.

Java Math.rint Function Example

The Java Math.rint function rounds the double value to the nearest integer. In this Java program, We are going to round both the positive and negative values and display the output.

package MathFunctions;

public class RintMethod {
	public static void main(String[] args) {
		double a = Math.rint(140.456 - 34.9865 - 4.52);
		System.out.println("Math.rint Result = " + a);

		System.out.println("\nMath.rint Result of Positive Number: " + Math.rint(0.25));
		System.out.println("Math.rint Result of Positive Number: " + Math.rint(12.05));
		System.out.println("Math.rint Result of Positive Number: " + Math.rint(12.85));
		
		System.out.println("\nMath.rint Result of Negative Number: " + Math.rint(-10.85));
		System.out.println("RMath.rint Result of Negative Number: " + Math.rint(-10.25));
	}
}

OUTPUT

Java rint Function 1

ANALYSIS

First, We declared a variable of type Double and performed the Java rint function directly on expression.

double a = Math.rint(140.456 - 34.9865 - 4.52);
System.out.println("Math.rint Result = " + a);

Next, We used the Math.rint Function directly on Positive double values.

System.out.println("\nMath.rint Result of Positive Number: " + Math.rint(0.25));
System.out.println("Math.rint Result of Positive Number: " + Math.rint(12.05));
System.out.println("Math.rint Result of Positive Number: " + Math.rint(12.85));

Next, We used the rint Math function directly on Negative double values.

System.out.println("\nMath.rint Result of Negative Number: " + Math.rint(-10.85));
System.out.println("RMath.rint Result of Negative Number: " + Math.rint(-10.25));

Java Math.rint on Array example

In this Java program, we find the rounded values of bulk data. Here, we are going to declare an array of double type and find the closest (rounded) values of array elements.

package MathFunctions;

public class RintMethodOnArrays {
	public static void main(String[] args) {
		
		double [] rintArray = {-19.25, 110.98, 144.21, -120.59, -605.87, 38.4897};

		for (int i = 0; i < rintArray.length; i++) {
			System.out.println("Math.rint Result of Array Element = " + Math.rint(rintArray[i]));
		}
	}
}

OUTPUT

Java rint Function 2

ANALYSIS

We used the For Loop to iterate the Java Array. Within the rint For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < rintArray.length).

TIP: rintArray.length finds the length of an array.

for (int i = 0; i < rintArray.length; i++) {

Here, we used the Math.rint Function directly inside the System.out.format statement. Here, the compiler will call the static double rint(double number) to find the corresponding closet values and print the output.

System.out.println("Math.rint Result of Array Element = " + Math.rint(rintArray[i]));

NOTE: To find the closet value of a single item, then use: Math.rint(myArray[index_position])

Java Math.rint function on Arraylist example

In this Java program, we are going to declare an ArrayList of double type and find the closet values of list elements.

package MathFunctions;

import java.util.ArrayList;

public class RintMethodOnList {
	public static void main(String[] args) {
		
		ArrayList<Double> myList = new ArrayList<Double>(5);
		myList.add(-2.289);
		myList.add(-2.65);
		myList.add(Math.PI);
		myList.add(59.05);
		myList.add(33.568);
		
		for (double x : myList) {
			System.out.println("Math.rint Result of ArrayList =  " + Math.rint(x));
		}
	}
}

OUTPUT

Java rint Function 3

ANALYSIS

We used the For Loop to iterate the double values in ArrayList

for (double x : myList) {

Here, we used the Math.rint Function directly inside the System.out.format statement. Here, the compiler will call the math.rint method ( static double rint(double x) ) to find the corresponding closest values and prints the output.

System.out.println("Math.rint Result of ArrayList =  " + Math.rint(x));

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy