TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Math.ceil Method

Use the Math.ceil method to compute ceiling functions for floating-point numbers.
Math.ceil. Integers have no fractional parts. With the floor and ceiling functions we compute integers. A floor is the lower integer, and a ceiling is the higher one.Math.floorMath
To compute a ceiling, we invoke Math.ceil. This method receives, and returns, a double value. If passed a number with no fractional part, that number is returned unchanged.
First example. With this method, a number is always increased if it has a fractional part. It does not matter how close to the lower number the number is. It is increased by ceil().

Double: The important thing to understand about Math.ceil is that it receives a double and returns a double.

Note: We can pass a float to Math.ceil and the Java compiler will transform it to a double implicitly.

Java program that uses Math.ceil import java.lang.Math; public class Program { public static void main(String[] args) { // Compute ceilings of these values. double value = Math.ceil(1.1); double value2 = Math.ceil(-0.9); System.out.println(value); System.out.println(value2); } } Output 2.0 -0.0
Ceiling, floor study. This program studies the result of floor and ceil. For numbers with no fractional part (like 1.0 or 2.0) we find that the ceiling and floor are equal.

Thus: There is no point in calling ceil or floor() on a number we already know has no fractional part.

Java program that computes floors and ceilings public class Program { public static void main(String[] args) { // Analyze these numbers. double[] values = { 1.0, 1.1, 1.5, 1.9, 2.0 }; for (double value : values) { // Compute the floor and the ceil for the number. double floor = Math.floor(value); double ceil = Math.ceil(value); // See if the floor equals the ceil. boolean equal = floor == ceil; // Print the values. System.out.println(value + ", Floor = " + floor + ", Ceil = " + ceil + ", Equal = " + equal); } } } Output 1.0, Floor = 1.0, Ceil = 1.0, Equal = true 1.1, Floor = 1.0, Ceil = 2.0, Equal = false 1.5, Floor = 1.0, Ceil = 2.0, Equal = false 1.9, Floor = 1.0, Ceil = 2.0, Equal = false 2.0, Floor = 2.0, Ceil = 2.0, Equal = true
With the Math class, we gain many powerful and well-tested methods. Math.ceil is similar to Math.floor. And programs that use one often use the other.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf