php 如何获得一个数字的整数部分和小数部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6619377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get whole and decimal part of a number?
提问by StackOverflowNewbie
Given, say, 1.25 - how do I get "1" and ."25" parts of this number?
给定,比如说,1.25 - 我如何得到这个数字的“1”和“25”部分?
I need to check if the decimal part is .0, .25, .5, or .75.
我需要检查小数部分是 .0、.25、.5 还是 .75。
回答by Brad Christie
$n = 1.25;
$whole = floor($n); // 1
$fraction = $n - $whole; // .25
Then compare against 1/4, 1/2, 3/4, etc.
然后与 1/4、1/2、3/4 等进行比较。
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.25in to -1& -0.25
在$returnUnsigned
作出停止其-1.25到-1和-0.25
回答by shelhamer
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.
其中 $whole 是整数,$decimal 将包含小数点后的数字。
回答by alex
回答by thedude
The floor() method doesn't work for negative numbers. This works every time:
floor() 方法不适用于负数。这每次都有效:
$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
回答by mpalencia
a short way (use floor and fmod)
一条捷径(使用 floor 和 fmod)
$var = "1.25";
$whole = floor($var); // 1
$decimal = fmod($var, 1); //0.25
then compare $decimal to 0, .25, .5, or .75
然后将 $decimal 与 0、.25、.5 或 .75 进行比较
回答by Dom
Cast it as an int and subtract
将其转换为 int 并减去
$integer = (int)$your_number;
$decimal = $your_number - $integer;
Or just to get the decimal for comparison
或者只是为了得到小数进行比较
$decimal = $your_number - (int)$your_number
回答by ip512
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.
还有一个 fmod 函数,可以使用: fmod($my_var, 1) 将返回相同的结果,但有时会出现一个小的圆形错误。
回答by Vasil Nikolov
PHP 5.4+
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
回答by Serdar De?irmenci
This is the way which I use:
这是我使用的方式:
$float = 4.3;
$dec = ltrim(($float - floor($float)),"0."); // result .3
回答by GordonM
Brad Christie's method is essentially correct but it can be written more concisely.
Brad Christie 的方法本质上是正确的,但可以写得更简洁。
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.
这相当于他的方法,但更短,因此更容易理解。