What's the best way to get the fractional part of a float in PHP?

Php

Php Problem Overview


How would you find the fractional part of a floating point number in PHP?

For example, if I have the value 1.25, I want to return 0.25.

Php Solutions


Solution 1 - Php

$x = $x - floor($x)

Solution 2 - Php

$x = fmod($x, 1);

Here's a demo:

<?php
$x = 25.3333;
$x = fmod($x, 1);
var_dump($x);

Should ouptut

double(0.3333)

Credit.

Solution 3 - Php

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 http://us2.php.net/manual/en/intro.bc.php">BCMath Arbitrary Precision Mathematics functions.

$x = 22.732423423423432;
$x = bcsub(abs($x),floor(abs($x)),20);

You could also hack on the string yourself

$x = 22.732423423423432;    
$x = strstr ( $x, '.' );

Solution 4 - Php

If if the number is negative, you'll have to do this:

 $x = abs($x) - floor(abs($x));

Solution 5 - Php

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:

$x = $x - intval($x)

Solution 6 - Php

My PHP skills are lacking but you could minus the result of a floor from the original number

Solution 7 - Php

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.

i.e:

  • .25 : 0 is integer below, fractional part is .25
  • -.25 : -1 is integer below, fractional part is .75

With the other solutions, you will repeat 0 as integer below, and worse, you will get reversed fractional values for all negative numbers.

Solution 8 - Php

Some of the preceding answers are partial. This, I believe, is what you need to handle all situations:

function getDecimalPart($floatNum) {
    return abs($floatNum - intval($floatNum));
}

$decimalPart = getDecimalPart($floatNum);

Solution 9 - Php

You can use fmod function:

$y = fmod($x, 1); //$x = 1.25 $y = 0.25

Solution 10 - Php

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:

$frac=($x<0) ? $x-ceil($x) : $x-floor($x);

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.

$frac=($x<0) ? $x-ceil($x) : $x-floor($x); -> 490-510 ms (depending on the sign of $x)

$frac=fmod($x, 1); -> 590 - 1000 ms (depending on the value of $x)

Whereas the actual empty loop itself takes 80 ms (which is included in above timings).

Test script:

$x=sqrt(2)-0.41421356237;

$time_start = microtime(true);
for ($i=0;$i<=9999999;$i++) {
    //$frac=fmod($x, 1); // version a
    $frac=($x<0) ? $x-ceil($x) : $x-floor($x); // version b
}
$time_end = microtime(true);

$time = $time_end - $time_start;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontkrehbielView Question on Stackoverflow
Solution 1 - PhpnlucaroniView Answer on Stackoverflow
Solution 2 - PhpsanmaiView Answer on Stackoverflow
Solution 3 - PhpAlan StormView Answer on Stackoverflow
Solution 4 - PhpPaige RutenView Answer on Stackoverflow
Solution 5 - PhpMichael FenwickView Answer on Stackoverflow
Solution 6 - PhpEthan GundersonView Answer on Stackoverflow
Solution 7 - Phpuser1091700View Answer on Stackoverflow
Solution 8 - PhpPaul MView Answer on Stackoverflow
Solution 9 - PhpdmvslvView Answer on Stackoverflow
Solution 10 - PhpSirDagenView Answer on Stackoverflow