tkrehbiel
tkrehbiel

Reputation: 780

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

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.

Upvotes: 55

Views: 40535

Answers (10)

SirDagen
SirDagen

Reputation: 41

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;

Upvotes: 0

dmvslv
dmvslv

Reputation: 381

You can use fmod function:

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

Upvotes: 1

sanmai
sanmai

Reputation: 30911

$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.

Upvotes: 24

Paul M
Paul M

Reputation: 37

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

Upvotes: 1

user1091700
user1091700

Reputation:

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.

Upvotes: 1

Michael Fenwick
Michael Fenwick

Reputation: 2504

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)

Upvotes: 11

Alana Storm
Alana Storm

Reputation: 166106

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, '.' );

Upvotes: 15

Paige Ruten
Paige Ruten

Reputation: 176743

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

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

Upvotes: 9

Ethan Gunderson
Ethan Gunderson

Reputation: 11517

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

Upvotes: 3

nlucaroni
nlucaroni

Reputation: 47934

$x = $x - floor($x)

Upvotes: 79

Related Questions