52

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.

10 Answers 10

79
$x = $x - floor($x)
4
  • this won't work when you are dealing with float number like = 0.25 Sep 6, 2012 at 12:14
  • 5
    @primo: put your comment into an answer, you deserve to increase your reputation for that! :) Sep 19, 2013 at 12:58
  • 13
    As a comment to the accepted answer, it is more likely to be seen, and is therefore more useful.
    – primo
    Sep 20, 2013 at 5:54
  • $a= fmod(1.01,1)*100; echo $a; echo PHP_EOL; $rounded= fmod($a,1); echo $rounded; 1 8.8817841970013E-16 Works strange for me
    – Tebe
    Apr 28, 2017 at 18:21
22
$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.

1
  • Probably the best answer, as doesn't require the value to be used twice, necessitating storage. To ensure no sign on the number, wrap in abs(), so it's: abs(fmod($x, 1))
    – Orbling
    Nov 19, 2018 at 21:45
15

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 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, '.' );
1
  • In a number like 10000000000000.00011111 it does not work :( Sep 25, 2020 at 14:10
10

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)
2
  • 1
    Just a note: typecasting to (int) would be faster
    – Prasad
    Apr 14, 2012 at 20:47
  • This solution provides result for all aspects wether it's less than 1 or not. Nov 28, 2022 at 6:40
9

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

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

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

1

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.

1

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);
1

You can use fmod function:

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

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.