<p>What is the difference between <a href="http://msdn.microsoft.com/en-us/library/9a6a2sxy.aspx" rel="noreferrer"><code>Math.Floor()</code></a> and <a href="http://msdn.microsoft.com/en-us/library/system.math.truncate.aspx" rel="noreferrer"><code>Math.Truncate()</code></a> in .NET?</p>

[{'answer_id': 33, 'author': 'C. K. Young', 'author_id': 13, 'author_profile': 'https://Stackoverflow.com/users/13', 'pm_score': 9, 'selected': False, 'text': '<p><code>Math.Floor</code> rounds down, <code>Math.Ceiling</code> rounds up, and <code>Math.Truncate</code> rounds towards zero. Thus, <code>Math.Truncate</code> is like <code>Math.Floor</code> for positive numbers, and like <code>Math.Ceiling</code> for negative numbers. Here\'s the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.math.truncate" rel="noreferrer">reference</a>.</p>\n<p>For completeness, <code>Math.Round</code> rounds to the nearest integer. If the number is exactly midway between two integers, then it rounds towards the even one. <a href="https://learn.microsoft.com/en-us/dotnet/api/system.math.round" rel="noreferrer">Reference.</a></p>\n<p>See also: <a href="https://stackoverflow.com/questions/14/whats-the-difference-between-math-floor-and-math-truncate-in-c/580252#580252">Pax Diablo\'s answer</a>. Highly recommended!</p>\n'}, {'answer_id': 2086, 'author': 'Marek Grzenkowicz', 'author_id': 95, 'author_profile': 'https://Stackoverflow.com/users/95', 'pm_score': 6, 'selected': False, 'text': '<p>Some examples:</p>\n\n<pre><code>Round(1.5) = 2\nRound(2.5) = 2\nRound(1.5, MidpointRounding.AwayFromZero) = 2\nRound(2.5, MidpointRounding.AwayFromZero) = 3\nRound(1.55, 1) = 1.6\nRound(1.65, 1) = 1.6\nRound(1.55, 1, MidpointRounding.AwayFromZero) = 1.6\nRound(1.65, 1, MidpointRounding.AwayFromZero) = 1.7\n\nTruncate(2.10) = 2\nTruncate(2.00) = 2\nTruncate(1.90) = 1\nTruncate(1.80) = 1\n</code></pre>\n'}, {'answer_id': 580252, 'author': 'paxdiablo', 'author_id': 14860, 'author_profile': 'https://Stackoverflow.com/users/14860', 'pm_score': 9, 'selected': False, 'text': '<p>Follow these links for the MSDN descriptions of:</p>\n<ul>\n<li><a href="https://learn.microsoft.com/en-us/dotnet/api/system.math.floor" rel="noreferrer"><code>Math.Floor</code></a>, which rounds down towards negative infinity.</li>\n<li><a href="https://learn.microsoft.com/en-us/dotnet/api/system.math.ceiling" rel="noreferrer"><code>Math.Ceiling</code></a>, which rounds up towards positive infinity.</li>\n<li><a href="https://learn.microsoft.com/en-us/dotnet/api/system.math.truncate" rel="noreferrer"><code>Math.Truncate</code></a>, which rounds up or down towards zero.</li>\n<li><a href="https://learn.microsoft.com/en-us/dotnet/api/system.math.round" rel="noreferrer"><code>Math.Round</code></a>, which rounds to the nearest integer or specified number of decimal places. You can specify the behavior if it\'s exactly equidistant between two possibilities, such as rounding so that the final digit is even (&quot;<code>Round(2.5,MidpointRounding.ToEven)</code>&quot; becoming 2) or so that it\'s further away from zero (&quot;<code>Round(2.5,MidpointRounding.AwayFromZero)</code>&quot; becoming 3).</li>\n</ul>\n<p>The following diagram and table may help:</p>\n<pre><code>-3        -2        -1         0         1         2         3\n +--|------+---------+----|----+--|------+----|----+-------|-+\n    a                     b       c           d            e\n\n                       a=-2.7  b=-0.5  c=0.3  d=1.5  e=2.8\n                       ======  ======  =====  =====  =====\nFloor                    -3      -1      0      1      2\nCeiling                  -2       0      1      2      3\nTruncate                 -2       0      0      1      2\nRound (ToEven)           -3       0      0      2      3\nRound (AwayFromZero)     -3      -1      0      2      3\n</code></pre>\n<p>Note that <code>Round</code> is a lot more powerful than it seems, simply because it can round to a specific number of decimal places. All the others round to zero decimals always. For example:</p>\n<pre><code>n = 3.145;\na = System.Math.Round (n, 2, MidpointRounding.ToEven);       // 3.14\nb = System.Math.Round (n, 2, MidpointRounding.AwayFromZero); // 3.15\n</code></pre>\n<p>With the other functions, you have to use multiply/divide trickery to achieve the same effect:</p>\n<pre><code>c = System.Math.Truncate (n * 100) / 100;                    // 3.14\nd = System.Math.Ceiling (n * 100) / 100;                     // 3.15\n</code></pre>\n'}, {'answer_id': 6742125, 'author': 'Azhar', 'author_id': 228755, 'author_profile': 'https://Stackoverflow.com/users/228755', 'pm_score': 6, 'selected': False, 'text': '<p><code>Math.Floor()</code> rounds toward negative infinity </p>\n\n<p><code>Math.Truncate</code> rounds up or down towards zero.</p>\n\n<p>For example:</p>\n\n<pre><code>Math.Floor(-3.4)     = -4\nMath.Truncate(-3.4)  = -3\n</code></pre>\n\n<p>while </p>\n\n<pre><code>Math.Floor(3.4)     = 3\nMath.Truncate(3.4)  = 3\n</code></pre>\n'}, {'answer_id': 10937469, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/system.math.floor.aspx" rel="noreferrer"><code>Math.Floor()</code></a> rounds \n"toward negative infinity" in compliance to <a href="http://standards.ieee.org/findstds/standard/754-2008.html" rel="noreferrer">IEEE Standard 754</a> section 4.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.math.truncate.aspx" rel="noreferrer"><code>Math.Truncate()</code></a> rounds " to the nearest integer towards zero." </p>\n'}, {'answer_id': 18893721, 'author': 'Pinky', 'author_id': 2699939, 'author_profile': 'https://Stackoverflow.com/users/2699939', 'pm_score': 4, 'selected': False, 'text': '<p><code>Math.Floor()</code>: Returns the largest integer less than or equal to the specified double-precision floating-point number.</p>\n\n<p><code>Math.Round()</code>: Rounds a value to the nearest integer or to the specified number of fractional digits.</p>\n'}, {'answer_id': 27742779, 'author': 'Sandesh', 'author_id': 1018966, 'author_profile': 'https://Stackoverflow.com/users/1018966', 'pm_score': 5, 'selected': False, 'text': '<p>They are functionally equivalent with positive numbers. The difference is in how they handle negative numbers.</p>\n\n<p>For example:</p>\n\n<pre><code>Math.Floor(2.5) = 2\nMath.Truncate(2.5) = 2\n\nMath.Floor(-2.5) = -3\nMath.Truncate(-2.5) = -2\n</code></pre>\n\n<p>MSDN links:\n - <a href="http://msdn.microsoft.com/en-us/library/e0b5f0xb.aspx" rel="noreferrer">Math.Floor Method</a>\n - <a href="http://msdn.microsoft.com/en-us/library/c2eabd70.aspx" rel="noreferrer">Math.Truncate Method</a></p>\n\n<p>P.S. Beware of Math.Round it may not be what you expect.</p>\n\n<p>To get the "standard" rounding result use:</p>\n\n<pre><code>float myFloat = 4.5;\nConsole.WriteLine( Math.Round(myFloat) ); // writes 4\nConsole.WriteLine( Math.Round(myFloat, 0, MidpointRounding.AwayFromZero) ) //writes 5\nConsole.WriteLine( myFloat.ToString("F0") ); // writes 5\n</code></pre>\n'}, {'answer_id': 35358670, 'author': 'safin chacko', 'author_id': 5815959, 'author_profile': 'https://Stackoverflow.com/users/5815959', 'pm_score': 5, 'selected': False, 'text': '<p>Try this, Examples: </p>\n\n<p><strong><em>Math.Floor()</em></strong> vs  <strong><em>Math.Truncate()</em></strong></p>\n\n<pre><code>Math.Floor(2.56) = 2\nMath.Floor(3.22) = 3\nMath.Floor(-2.56) = -3\nMath.Floor(-3.26) = -4\n\nMath.Truncate(2.56) = 2\nMath.Truncate(2.00) = 2\nMath.Truncate(1.20) = 1\nMath.Truncate(-3.26) = -3\nMath.Truncate(-3.96) = -3\n</code></pre>\n\n<p>Also <strong><em>Math.Round()</em></strong></p>\n\n<pre><code>   Math.Round(1.6) = 2\n   Math.Round(-8.56) = -9\n   Math.Round(8.16) = 8\n   Math.Round(8.50) = 8\n   Math.Round(8.51) = 9\n</code></pre>\n\n<p><strong><code>math.floor()</code></strong></p>\n\n<p>Returns the largest integer less than or equal to the specified number.\nMSDN <a href="https://msdn.microsoft.com/en-us/library/system.math.floor.aspx" rel="noreferrer">system.math.floor</a></p>\n\n<p><strong><code>math.truncate()</code></strong></p>\n\n<p>Calculates the integral part of a number.\nMSDN <a href="https://msdn.microsoft.com/en-us/library/system.math.truncate(v=vs.110).aspx" rel="noreferrer">system.math.truncate</a></p>\n'}, {'answer_id': 48732884, 'author': 'Puddle', 'author_id': 9312988, 'author_profile': 'https://Stackoverflow.com/users/9312988', 'pm_score': 6, 'selected': False, 'text': "<p><code>Math.floor</code> sliiiide to the left...<br />\n<code>Math.ceil</code> sliiiide to the right...<br />\n<code>Math.truncate</code> criiiiss crooooss (floor/ceil always towards 0)<br />\n<code>Math.round</code> cha cha, real smooth... (go to closest side)</p>\n<p>Let's go to work! (⌐□_□)</p>\n<p>To the left... <code>Math.floor</code><br />\nTake it back now y'all... <code>--</code><br />\nTwo hops this time... <code>-=2</code></p>\n<p>Everybody clap your hands ✋✋</p>\n<p>How low can you go? Can you go down low? All the way to the <code>floor</code>?</p>\n<pre><code>if (this == &quot;wrong&quot;)\n    return &quot;i don't wanna be right&quot;;\n</code></pre>\n<p><code>Math.truncate(x)</code> is also the same as <code>int(x)</code>.<br />\nby removing a positive or negative fraction, you're always heading towards 0.</p>\n"}, {'answer_id': 51375320, 'author': 'vidy', 'author_id': 9993935, 'author_profile': 'https://Stackoverflow.com/users/9993935', 'pm_score': 3, 'selected': False, 'text': '<p><strong><code>Math.floor()</code></strong> will always round down ie., it returns LESSER integer. While <strong><code>round()</code></strong> will return the NEAREST integer</p>\n\n<p><strong><em>math.floor()</em></strong></p>\n\n<p>Returns the largest integer less than or equal to the specified number.</p>\n\n<p><strong><em>math.truncate()</em></strong></p>\n\n<p>Calculates the integral part of a number.</p>\n'}, {'answer_id': 60803832, 'author': 'Paul Moore', 'author_id': 4618316, 'author_profile': 'https://Stackoverflow.com/users/4618316', 'pm_score': 1, 'selected': False, 'text': '<p>Truncate drops the decimal point.</p>\n'}, {'answer_id': 62551075, 'author': 'Anonymous', 'author_id': 13127212, 'author_profile': 'https://Stackoverflow.com/users/13127212', 'pm_score': 2, 'selected': False, 'text': '<p>Math.Floor() :</p>\n<p>It gives the largest integer less than or equal to the given number.</p>\n<pre><code>    Math.Floor(3.45) =3\n    Math.Floor(-3.45) =-4\n</code></pre>\n<p>Math.Truncate():</p>\n<p>It removes the decimal places of the number and replace with zero</p>\n<pre><code>Math.Truncate(3.45)=3\n Math.Truncate(-3.45)=-3\n</code></pre>\n<p>Also from above examples we can see that floor and truncate are same for positive numbers.</p>\n'}, {'answer_id': 65052124, 'author': 'Abhishek Choudhary', 'author_id': 7946599, 'author_profile': 'https://Stackoverflow.com/users/7946599', 'pm_score': 1, 'selected': False, 'text': '<p>Going by the Mathematical Definition of Floor, that is, &quot;Greatest integer less than or equal to a number&quot;, This is completely unambiguous, whereas, Truncate just removes the fractional part, which is equivalent to round towards 0.</p>\n'}]
['https://Stackoverflow.com/questions/14', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11/']