123

Given, say, 1.25 - how do I get "1" and ."25" parts of this number?

I need to check if the decimal part is .0, .25, .5, or .75.

5
  • Will a simple explode work? $splitDec = explode(".", $decimal); $splitDec[0] will now by the 1 and splitDec[1] will now be 25 from your example.
    – Matt
    Jul 8, 2011 at 2:35
  • @Matt split() has been deprecated.
    – alex
    Jul 8, 2011 at 2:36
  • @alex yea, forgot. Was thinking javascript. changed to explode.
    – Matt
    Jul 8, 2011 at 2:37
  • @Matt, split has been deprecated as of PHP 5.3 and actually takes a regex as its arg, whereas explode takes a string. explode() is preferred in this case.
    – shelhamer
    Jul 8, 2011 at 2:37
  • 4
    Be aware that explode(".",1.10); wil give 1 and 1, not 1 and 10
    – Michel
    Oct 8, 2013 at 10:29

20 Answers 20

216
$n = 1.25;
$whole = floor($n);      // 1
$fraction = $n - $whole; // .25

Then compare against 1/4, 1/2, 3/4, etc.


In cases of negative numbers, use this:

function NumberBreakdown($number, $returnUnsigned = false)
{
  $negative = 1;
  if ($number < 0)
  {
    $negative = -1;
    $number *= -1;
  }

  if ($returnUnsigned){
    return array(
      floor($number),
      ($number - floor($number))
    );
  }

  return array(
    floor($number) * $negative,
    ($number - floor($number)) * $negative
  );
}

The $returnUnsigned stops it from making -1.25 in to -1 & -0.25

6
  • 8
    As a side, intval() or simple casting as (int) might be more performant than floor() Jul 8, 2011 at 2:36
  • 1
    How does floor account for negatives?
    – LanceH
    Jul 8, 2011 at 15:08
  • @LanceH: It doesn't, but you can make it handle it pretty easily with a negative flag. Jul 8, 2011 at 15:09
  • 4
    This answer is actually inaccurate unless you add a round(). Sounds dumb, I know, but it has to do with low-level floating point number arithmetic. See here: stackoverflow.com/questions/3726721/php-math-precision
    – Matt James
    Sep 12, 2014 at 18:56
  • Try with 510.9, returns 0.89. But can't tell why.
    – T30
    Feb 18, 2019 at 14:16
40

This code will split it up for you:

list($whole, $decimal) = explode('.', $your_number);

where $whole is the whole number and $decimal will have the digits after the decimal point.

5
  • 16
    Be aware that explode(".",1.10); wil give 1 and 1, not 1 and 10
    – Michel
    Oct 8, 2013 at 10:29
  • 4
    This breaks and throws warning about undefined index, if number is integer or does not contain decimal part.
    – Andreyco
    Nov 16, 2013 at 16:42
  • 1
    This assumes that the number is not going to end up being represented in scientific notation when cast to string. If it's a really really huge or really really tiny number then this won't be the case and this method will break.
    – GordonM
    Oct 4, 2015 at 9:37
  • It doesn't work when your number is : $value = 10000000000000.00011111; Sep 25, 2020 at 13:51
  • this works very well for numbers in outline form (x.y) - thanks
    – cloudxix
    Jul 14, 2022 at 8:42
32

The floor() method doesn't work for negative numbers. This works every time:

$num = 5.7;
$whole = (int) $num;  // 5
$frac  = $num - $whole;  // .7

...also works for negatives (same code, different number):

$num = -5.7;
$whole = (int) $num;  // -5
$frac  = $num - $whole;  // -.7
2
  • when the number is something like $value = 10000000000000.00011111; it does not work too Sep 25, 2020 at 13:55
  • @HamidNaghipour It does as long as your processor can handle enough bits. It's a limitation of the computer handling it as a numeric value instead of a string.
    – Eric
    Jun 29, 2022 at 20:26
23

Just to be different :)

list($whole, $decimal) = sscanf(1.5, '%d.%d');

CodePad.

As an added benefit, it will only split where both sides consist of digits.

6
  • 2
    This one does not break if number does not contain decimal part. Great!
    – Andreyco
    Nov 16, 2013 at 16:41
  • Nice that this outputs two integers; rather than an int and a float. Dec 12, 2013 at 21:31
  • 1
    This assumes that the number is not going to end up being represented in scientific notation when cast to string. If it's a really really huge or really really tiny number then this won't be the case and this method will break.
    – GordonM
    Oct 4, 2015 at 9:42
  • And this one dose not work when the number is like this: $value = 10000000000000.00011111; Sep 25, 2020 at 13:52
  • 1
    sscanf() first argument should be a string php.net/manual/fr/function.sscanf.php don't forget to cast your float to a string
    – Julien
    Dec 8, 2020 at 14:14
17

a short way (use floor and fmod)

$var = "1.25";
$whole = floor($var);     // 1
$decimal = fmod($var, 1); //0.25

then compare $decimal to 0, .25, .5, or .75

1
  • I accept this answer! It's close to C#'s (a % 1) AND it handles negative numbers well.
    – Bitterblue
    Jan 10, 2020 at 10:05
8

Cast it as an int and subtract

$integer = (int)$your_number;
$decimal = $your_number - $integer;

Or just to get the decimal for comparison

$decimal = $your_number - (int)$your_number
5

There's a fmod function too, that can be used : fmod($my_var, 1) will return the same result, but sometime with a small round error.

5

PHP 5.4+

$n = 12.343;
intval($n); // 12
explode('.', number_format($n, 1))[1]; // 3
explode('.', number_format($n, 2))[1]; // 34
explode('.', number_format($n, 3))[1]; // 343
explode('.', number_format($n, 4))[1]; // 3430
5

Just a new simple solution, for those of you who want to get the Integer part and Decimal part splitted as two integer separated values:

5.25 -> Int part: 5; Decimal part: 25

$num = 5.25;
$int_part = intval($num);
$dec_part = $num * 100 % 100;

This way is not involving string based functions, and is preventing accuracy problems which may arise in other math operations (such as having 0.49999999999999 instead of 0.5).

Haven't tested thoroughly with extreme values, but it works fine for me for price calculations.

But, watch out! Now from -5.25 you get: Integer part: -5; Decimal part: -25

In case you want to get always positive numbers, simply add abs() before the calculations:

$num = -5.25;
$num = abs($num);
$int_part = intval($num);
$dec_part = $num * 100 % 100;

Finally, bonus snippet for printing prices with 2 decimals:

$message = sprintf("Your price: %d.%02d Eur", $int_part, $dec_part);

...so that you avoid getting 5.5 instead of 5.05. ;)

2
  • These techniques too rigidly require two decimal places in the input. Oct 21, 2021 at 2:51
  • The code did not work with '154.45' it returns 44. Dec 27, 2021 at 11:26
2

This is the way which I use:

$float = 4.3;    

$dec = ltrim(($float - floor($float)),"0."); // result .3
1

Brad Christie's method is essentially correct but it can be written more concisely.

function extractFraction ($value) 
{
    $fraction   = $value - floor ($value);
    if ($value < 0)
    {
        $fraction *= -1;
    }

    return $fraction;
}

This is equivalent to his method but shorter and hopefully easier to understand as a result.

1

I was having a hard time finding a way to actually separate the dollar amount and the amount after the decimal. I think I figured it out mostly and thought to share if any of yall were having trouble

So basically...

if price is 1234.44... whole would be 1234 and decimal would be 44 or

if price is 1234.01... whole would be 1234 and decimal would be 01 or

if price is 1234.10... whole would be 1234 and decimal would be 10

and so forth

$price = 1234.44;

$whole = intval($price); // 1234
$decimal1 = $price - $whole; // 0.44000000000005 uh oh! that's why it needs... (see next line)
$decimal2 = round($decimal1, 2); // 0.44 this will round off the excess numbers
$decimal = substr($decimal2, 2); // 44 this removed the first 2 characters

if ($decimal == 1) { $decimal = 10; } // Michel's warning is correct...
if ($decimal == 2) { $decimal = 20; } // if the price is 1234.10... the decimal will be 1...
if ($decimal == 3) { $decimal = 30; } // so make sure to add these rules too
if ($decimal == 4) { $decimal = 40; }
if ($decimal == 5) { $decimal = 50; }
if ($decimal == 6) { $decimal = 60; }
if ($decimal == 7) { $decimal = 70; }
if ($decimal == 8) { $decimal = 80; }
if ($decimal == 9) { $decimal = 90; }

echo 'The dollar amount is ' . $whole . ' and the decimal amount is ' . $decimal;
1
$x = 1.24

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

echo $result; // .24
1

If you can count on it always having 2 decimal places, you can just use a string operation:

$decimal = 1.25;
substr($decimal,-2);  // returns "25" as a string

No idea of performance but for my simple case this was much better...

1
  • Note: if you do this, you should at least use round($decimal, 2); first to ensure you only have two digits as decimal. But you can not use this if you want to have correct math or handle money. Jul 23, 2020 at 19:43
0

To prevent the extra float decimal (i.e. 50.85 - 50 give 0.850000000852), in my case I just need 2 decimals for money cents.

$n = 50.85;
$whole = intval($n);
$fraction = $n * 100 % 100;
1
  • This answer is not suitable for floats with more or less than 2 decimal places. 3v4l.org/An9gO Oct 21, 2021 at 2:50
0

Try it this way... it's easier like this

$var = "0.98";

$decimal = strrchr($var,".");

$whole_no = $var-$decimal;

echo $whole_no;

echo str_replace(".", "", $decimal);
0

You could also use something like this:

preg_match("/([0-9]+)\.([0-9]+)/", $number, $matches);
0

If you want the two halves to be explicitly type cast, then sscanf() is a great call.

Code: (Demo)

var_dump(sscanf(1.25, '%d%f'));

Output:

array(2) {
  [0]=>
  int(1)
  [1]=>
  float(0.25)
}

Or you can assign the two variables individually:

sscanf(1.25, '%d%f', $int, $float);
var_dump($int);
var_dump($float);

Casting the decimal portion as a float is particularly useful when, say, converting decimal expression of hours to hours and minutes. (Demo)

$decimalTimes = [
    6,
    7.2,
    8.78,
];

foreach ($decimalTimes as $decimalTime) {
    sscanf($decimalTime, '%d%f', $hours, $minutes);
    printf('%dh%02dm', $hours, round($minutes * 60));
    echo "\n";
}

Output:

6h00m
7h12m
8h47m  // if round() was not used, this would be 8h46m
0
-1

Not seen a simple modulus here...

$number         = 1.25;
$wholeAsFloat   = floor($number);   // 1.00
$wholeAsInt     = intval($number);  // 1
$decimal        = $number % 1;      // 0.25

In this case getting both $wholeAs? and $decimal don't depend on the other. (You can just take 1 of the 3 outputs independently.) I've shown $wholeAsFloat and $wholeAsInt because floor() returns a float type number even though the number it returns will always be whole. (This is important if you're passing the result into a type-hinted function parameter.)

I wanted this to split a floating point number of hours/minutes, e.g. 96.25, into hours and minutes separately for a DateInterval instance as 96 hours 15 minutes. I did this as follows:

$interval = new \DateInterval(sprintf("PT%dH%dM", intval($hours), (($hours % 1) * 60)));

I didn't care about seconds in my case.

0
-3
val = -3.1234

fraction = abs(val - as.integer(val) ) 
1
  • 2
    Hi, do add a bit of explanation along with the code as it helps to understand your code. Code only answers are frowned upon. Jan 10, 2016 at 17:07

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.