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');
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
Related videos on Youtube

Author by
StackOverflowNewbie
Updated on October 22, 2021Comments
-
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 over 11 yearsWill 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 over 11 years@alex yea, forgot. Was thinking javascript. changed to explode.
-
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 about 9 yearsBe aware that
explode(".",1.10);
wil give 1 and 1, not 1 and 10
-
-
Jason McCreary over 11 yearsAs a side,
intval()
or simple casting as(int)
might be more performant thanfloor()
-
LanceH over 11 yearsHow does floor account for negatives?
-
Brad Christie over 11 years@LanceH: It doesn't, but you can make it handle it pretty easily with a negative flag.
-
Michel about 9 yearsBe aware that
explode(".",1.10);
wil give 1 and 1, not 1 and 10 -
Andreyco about 9 yearsThis one does not break if number does not contain decimal part. Great!
-
Andreyco about 9 yearsThis breaks and throws warning about undefined index, if number is integer or does not contain decimal part.
-
The Thirsty Ape about 9 yearsNice that this outputs two integers; rather than an int and a float.
-
Matt James over 8 yearsThis 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 about 7 yearsThis 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 about 7 yearsThis 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 almost 7 yearsHi, do add a bit of explanation along with the code as it helps to understand your code. Code only answers are frowned upon.
-
T30 almost 4 yearsTry with 510.9, returns 0.89. But can't tell why.
-
Bitterblue almost 3 yearsI accept this answer! It's close to C#'s
(a % 1)
AND it handles negative numbers well. -
Ulrik McArdle over 2 yearsNote: 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 over 2 yearsIt doesn't work when your number is : $value = 10000000000000.00011111;
-
Hamid Naghipour over 2 yearsAnd this one dose not work when the number is like this: $value = 10000000000000.00011111;
-
Hamid Naghipour over 2 yearswhen the number is something like $value = 10000000000000.00011111; it does not work too
-
Julien about 2 yearssscanf() first argument should be a string php.net/manual/fr/function.sscanf.php don't forget to cast your float to a string
-
mickmackusa about 1 yearThis answer is not suitable for floats with more or less than 2 decimal places. 3v4l.org/An9gO
-
mickmackusa about 1 yearThese techniques too rigidly require two decimal places in the input.
-
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 yearThe code did not work with '154.45' it returns 44.
-
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.