{"id":2048,"date":"2026-11-20T11:44:05","date_gmt":"2026-11-20T11:44:05","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=2048"},"modified":"2026-11-20T11:44:23","modified_gmt":"2026-11-20T11:44:23","slug":"java-math-floor","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/java-math-floor\/","title":{"rendered":"Java Math.floor() Method with Examples"},"content":{"rendered":"<p><code>Math.floor()<\/code> returns the <strong>largest whole number that is less than or equal to<\/strong> the value you pass. In simple words, it always rounds <strong>down<\/strong>, towards negative infinity. It returns a <code>double<\/code>.<\/p>\n<pre class=\"wp-block-code\"><code>public static double floor(double a)<\/code><\/pre>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>public class FloorDemo {\n    public static void main(String[] args) {\n        System.out.println(Math.floor(4.8));\n        System.out.println(Math.floor(23.99));\n        System.out.println(Math.floor(7.0));\n        System.out.println(Math.floor(-4.2));\n        System.out.println(Math.floor(-23.99));\n    }\n}<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>4.0\n23.0\n7.0\n-5.0\n-24.0<\/code><\/pre>\n<p>For positive numbers, <code>floor()<\/code> simply drops the decimal part. For <strong>negative<\/strong> numbers it moves further away from zero: the floor of <code>-4.2<\/code> is <code>-5.0<\/code>, because -5 is the largest whole number that is still less than -4.2.<\/p>\n<h2>floor() vs ceil() vs round() vs (int)<\/h2>\n<table>\n<thead>\n<tr>\n<th>Value<\/th>\n<th><code>Math.floor<\/code><\/th>\n<th><code>Math.ceil<\/code><\/th>\n<th><code>Math.round<\/code><\/th>\n<th><code>(int)<\/code> cast<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>4.8<\/td>\n<td>4.0<\/td>\n<td>5.0<\/td>\n<td>5<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>4.2<\/td>\n<td>4.0<\/td>\n<td>5.0<\/td>\n<td>4<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>-4.2<\/td>\n<td>-5.0<\/td>\n<td>-4.0<\/td>\n<td>-4<\/td>\n<td>-4<\/td>\n<\/tr>\n<tr>\n<td>-4.8<\/td>\n<td>-5.0<\/td>\n<td>-4.0<\/td>\n<td>-5<\/td>\n<td>-4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>(int)<\/code> cast <em>truncates<\/em> towards zero, which is different from <code>floor()<\/code> for negative numbers. Mixing them up is a common source of bugs.<\/p>\n<h2>Getting an int Instead of a double<\/h2>\n<pre class=\"wp-block-code\"><code>double price = 199.75;\nint rupees = (int) Math.floor(price);\nSystem.out.println(rupees);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>199<\/code><\/pre>\n<h2>Practical Example: Minutes to Hours and Minutes<\/h2>\n<pre class=\"wp-block-code\"><code>int totalMinutes = 157;\nint hours = (int) Math.floor(totalMinutes \/ 60.0);\nint minutes = totalMinutes % 60;\nSystem.out.println(hours + \" h \" + minutes + \" min\");<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>2 h 37 min<\/code><\/pre>\n<h2>The Integer Division Trap<\/h2>\n<pre class=\"wp-block-code\"><code>System.out.println(Math.floor(10 \/ 3));     \/\/ 10 \/ 3 is int division = 3\nSystem.out.println(Math.floor(-7 \/ 2));     \/\/ -7 \/ 2 is int division = -3\nSystem.out.println(Math.floorDiv(-7, 2));   \/\/ true floor division<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>3.0\n-3.0\n-4<\/code><\/pre>\n<p>When both numbers are integers, Java divides first (truncating towards zero) and <em>then<\/em> passes the result to <code>floor()<\/code>. So <code>Math.floor(-7 \/ 2)<\/code> gives <code>-3.0<\/code>, not <code>-4.0<\/code>. Use <code>-7 \/ 2.0<\/code>, or <code>Math.floorDiv()<\/code> for integers.<\/p>\n<h2>Special Cases<\/h2>\n<ul>\n<li>If the argument is already a whole number, the same value is returned (as a double).<\/li>\n<li><code>Math.floor(Double.NaN)<\/code> returns <code>NaN<\/code>, and infinity stays infinity.<\/li>\n<li><code>Math.floor(-0.5)<\/code> returns <code>-1.0<\/code>.<\/li>\n<\/ul>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What does Math.floor() return in Java?<\/h3>\n<p>A <code>double<\/code> holding the largest whole number less than or equal to the argument.<\/p>\n<h3>What is Math.floor(-2.5)?<\/h3>\n<p><code>-3.0<\/code>, because floor always rounds down.<\/p>\n<h3>How is Math.floor() different from casting to int?<\/h3>\n<p>Casting truncates towards zero, so <code>(int) -2.5<\/code> is <code>-2<\/code>, while <code>Math.floor(-2.5)<\/code> is <code>-3.0<\/code>.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/java-math\/\">Java Math class<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/arithmetic-operators-in-java\/\">Arithmetic operators in Java<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Math.floor() returns the largest whole number that is less than or equal to the value you pass. In simple words, it always rounds down, towards negative infinity. It returns a double. public static double floor(double a) Basic Example public class FloorDemo { public static void main(String[] args) { System.out.println(Math.floor(4.8)); System.out.println(Math.floor(23.99)); System.out.println(Math.floor(7.0)); System.out.println(Math.floor(-4.2)); System.out.println(Math.floor(-23.99)); } } &#8230; <a title=\"Java Math.floor() Method with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/java-math-floor\/\" aria-label=\"Read more about Java Math.floor() Method with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[12,11],"class_list":["post-2048","post","type-post","status-publish","format-standard","hentry","category-java-tutorial","tag-java","tag-java-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=2048"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2048\/revisions"}],"predecessor-version":[{"id":6149,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2048\/revisions\/6149"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=2048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=2048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=2048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}