<p>How would you find the fractional part of a floating point number in PHP?</p>
<p>For example, if I have the value <code>1.25</code>, I want to return <code>0.25</code>.</p>

[{'answer_id': 50806, 'author': 'nlucaroni', 'author_id': 157, 'author_profile': 'https://Stackoverflow.com/users/157', 'pm_score': 7, 'selected': True, 'text': '<pre><code>$x = $x - floor($x)\n</code></pre>\n'}, {'answer_id': 50807, 'author': 'Ethan Gunderson', 'author_id': 2066, 'author_profile': 'https://Stackoverflow.com/users/2066', 'pm_score': 2, 'selected': False, 'text': '<p>My PHP skills are lacking but you could minus the result of a floor from the original number</p>\n'}, {'answer_id': 50817, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 3, 'selected': False, 'text': "<p>If if the number is negative, you'll have to do this:</p>\n\n<pre><code> $x = abs($x) - floor(abs($x));\n</code></pre>\n"}, {'answer_id': 51909, 'author': 'Alan Storm', 'author_id': 4668, 'author_profile': 'https://Stackoverflow.com/users/4668', 'pm_score': 4, 'selected': False, 'text': '<p>Don\'t forget that you can\'t trust floating point arithmetic to be 100% accurate. If you\'re concerned about this, you\'ll want to look into the <a href="http://us2.php.net/manual/en/intro.bc.php" rel="noreferrer">BCMath Arbitrary Precision Mathematics</a> functions. </p>\n\n<pre><code>$x = 22.732423423423432;\n$x = bcsub(abs($x),floor(abs($x)),20);\n</code></pre>\n\n<p>You could also hack on the string yourself</p>\n\n<pre><code>$x = 22.732423423423432;    \n$x = strstr ( $x, \'.\' );\n</code></pre>\n'}, {'answer_id': 7734841, 'author': 'Michael Fenwick', 'author_id': 858061, 'author_profile': 'https://Stackoverflow.com/users/858061', 'pm_score': 3, 'selected': False, 'text': '<p>The answer provided by nlucaroni will only work for positive numbers.  A possible solution that works for both positive as well as negative numbers is:</p>\n\n<pre><code>$x = $x - intval($x)\n</code></pre>\n'}, {'answer_id': 8460196, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>However, if you are dealing with something like perlin noise or another graphical representation, the solution which was accepted is correct. It will give you the fractional part from the lower number. </p>\n\n<p>i.e:</p>\n\n<ul>\n<li><code>.25</code> : 0 is integer below, fractional part is .25</li>\n<li><code>-.25</code> : -1 is integer below, fractional part is .75</li>\n</ul>\n\n<p>With the other solutions, you will repeat 0 as integer below, and worse, you will get reversed fractional values for all negative numbers.</p>\n'}, {'answer_id': 38701237, 'author': 'Paul M', 'author_id': 933499, 'author_profile': 'https://Stackoverflow.com/users/933499', 'pm_score': 1, 'selected': False, 'text': '<p>Some of the preceding answers are partial. This, I believe, is what you need to handle <em>all</em> situations:</p>\n\n<pre><code>function getDecimalPart($floatNum) {\n    return abs($floatNum - intval($floatNum));\n}\n\n$decimalPart = getDecimalPart($floatNum);\n</code></pre>\n'}, {'answer_id': 41626638, 'author': 'sanmai', 'author_id': 93540, 'author_profile': 'https://Stackoverflow.com/users/93540', 'pm_score': 5, 'selected': False, 'text': '<pre><code>$x = fmod($x, 1);\n</code></pre>\n\n<p>Here\'s a demo:</p>\n\n<pre><code>&lt;?php\n$x = 25.3333;\n$x = fmod($x, 1);\nvar_dump($x);\n</code></pre>\n\n<p>Should ouptut</p>\n\n<pre><code>double(0.3333)\n</code></pre>\n\n<p><a href="https://stackoverflow.com/questions/50801/whats-the-best-way-to-get-the-fractional-part-of-a-float-in-php/41626638#comment17394709_50806">Credit.</a></p>\n'}, {'answer_id': 54575236, 'author': 'dmvslv', 'author_id': 3328002, 'author_profile': 'https://Stackoverflow.com/users/3328002', 'pm_score': 1, 'selected': False, 'text': '<p>You can use <a href="http://php.net/manual/en/function.fmod.php" rel="nofollow noreferrer">fmod</a> function:</p>\n\n<pre><code>$y = fmod($x, 1); //$x = 1.25 $y = 0.25\n</code></pre>\n'}, {'answer_id': 56249728, 'author': 'SirDagen', 'author_id': 11494956, 'author_profile': 'https://Stackoverflow.com/users/11494956', 'pm_score': 0, 'selected': False, 'text': '<p>To stop the confusion on this page actually this is the best answer, which is fast and works for both positive and negative values of $x:</p>\n\n<pre><code>$frac=($x&lt;0) ? $x-ceil($x) : $x-floor($x);\n</code></pre>\n\n<p>I ran speed tests of 10 million computations on PHP 7.2.15 and even though both solutions give the same results, fmod is slower than floor/ceil. </p>\n\n<p><code>$frac=($x&lt;0) ? $x-ceil($x) : $x-floor($x);</code>\n-> 490-510 ms (depending on the sign of $x) </p>\n\n<p><code>$frac=fmod($x, 1);</code>\n-> 590 - 1000 ms (depending on the value of $x) </p>\n\n<p>Whereas the actual empty loop itself takes 80 ms (which is included in above timings). </p>\n\n<p>Test script:</p>\n\n<pre><code>$x=sqrt(2)-0.41421356237;\n\n$time_start = microtime(true);\nfor ($i=0;$i&lt;=9999999;$i++) {\n    //$frac=fmod($x, 1); // version a\n    $frac=($x&lt;0) ? $x-ceil($x) : $x-floor($x); // version b\n}\n$time_end = microtime(true);\n\n$time = $time_end - $time_start;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/50801', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4925/']