How to get whole and decimal part of a number?

170,333

Solution 1

$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

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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

Share:
170,333

Related videos on Youtube

StackOverflowNewbie
Author by

StackOverflowNewbie

Updated on October 22, 2021

Comments

  • StackOverflowNewbie
    StackOverflowNewbie about 1 year

    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.

    • Matt
      Matt over 11 years
      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.
    • user3167101 over 11 years
      @Matt split() has been deprecated.
    • Matt
      Matt over 11 years
      @alex yea, forgot. Was thinking javascript. changed to explode.
    • shelhamer
      shelhamer over 11 years
      @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.
    • Michel
      Michel about 9 years
      Be aware that explode(".",1.10); wil give 1 and 1, not 1 and 10
  • Jason McCreary
    Jason McCreary over 11 years
    As a side, intval() or simple casting as (int) might be more performant than floor()
  • LanceH
    LanceH over 11 years
    How does floor account for negatives?
  • Brad Christie
    Brad Christie over 11 years
    @LanceH: It doesn't, but you can make it handle it pretty easily with a negative flag.
  • Michel
    Michel about 9 years
    Be aware that explode(".",1.10); wil give 1 and 1, not 1 and 10
  • Andreyco about 9 years
    This one does not break if number does not contain decimal part. Great!
  • Andreyco about 9 years
    This breaks and throws warning about undefined index, if number is integer or does not contain decimal part.
  • The Thirsty Ape about 9 years
    Nice that this outputs two integers; rather than an int and a float.
  • Matt James over 8 years
    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
  • GordonM
    GordonM about 7 years
    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
    GordonM about 7 years
    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.
  • Bhargav Rao
    Bhargav Rao almost 7 years
    Hi, do add a bit of explanation along with the code as it helps to understand your code. Code only answers are frowned upon.
  • T30
    T30 almost 4 years
    Try with 510.9, returns 0.89. But can't tell why.
  • Bitterblue
    Bitterblue almost 3 years
    I accept this answer! It's close to C#'s (a % 1) AND it handles negative numbers well.
  • Ulrik McArdle over 2 years
    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.
  • Hamid Naghipour
    Hamid Naghipour over 2 years
    It doesn't work when your number is : $value = 10000000000000.00011111;
  • Hamid Naghipour
    Hamid Naghipour over 2 years
    And this one dose not work when the number is like this: $value = 10000000000000.00011111;
  • Hamid Naghipour
    Hamid Naghipour over 2 years
    when the number is something like $value = 10000000000000.00011111; it does not work too
  • Julien
    Julien about 2 years
    sscanf() first argument should be a string php.net/manual/fr/function.sscanf.php don't forget to cast your float to a string
  • mickmackusa
    mickmackusa about 1 year
    This answer is not suitable for floats with more or less than 2 decimal places. 3v4l.org/An9gO
  • mickmackusa
    mickmackusa about 1 year
    These techniques too rigidly require two decimal places in the input.
  • mickmackusa
    mickmackusa about 1 year
    sscanf() performs greedy matching and variables can be assigned within the function call (list() is not required) stackoverflow.com/a/69039147/2943403
  • Alberto Guilherme about 1 year
    The code did not work with '154.45' it returns 44.
  • Eric
    Eric 6 months
    @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.