如何得到一个数的整数和小数部分?

假设是1.25-我怎样得到这个数的“1”和“25”部分?

我需要检查小数部分是.0、 .25、 .5还是.75。

181796 次浏览
$n = 1.25;
$whole = floor($n);      // 1
$fraction = $n - $whole; // .25

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


如果是负数,使用以下方法:

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

$returnUnsigned阻止它使 -1.25进入到 -1-0.25

这段代码将为您分割它:

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

其中 $whole 是整数,$decal 将有小数点后面的数字。

只是为了与众不同:)

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

CodePad.

作为一个额外的好处,它将只分裂两边都包含数字。

还有一个 fmod 函数,可以使用: Fmod ($my _ var,1) 将返回相同的结果,但有时有一个小的整数误差。

这是我使用的方式:

$float = 4.3;


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

将它强制转换为 int 并减去

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

Or just to get the decimal for comparison

$decimal = $your_number - (int)$your_number

布拉德克里斯蒂的方法基本上是正确的,但它可以写得更简洁。

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


return $fraction;
}

这与他的方法相当,但是更短,因此也更容易理解。

a short way (use floor and fmod)

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

然后将 $十进制与0、 .25、 .5或.75进行比较

val = -3.1234


fraction = abs(val - as.integer(val) )

Floor ()方法对负数不起作用,这种方法每次都起作用:

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

... 也适用于底片(同样的代码,不同的数字) :

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

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;

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

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

如果价格是1234.44整数是1234小数是44

如果价格是1234.01整数是1234小数是01或

如果价格是1234.10整数是1234小数是10

等等

$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;
$x = 1.24


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


echo $result; // .24

在这里没有看到一个简单的模数。

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

在这种情况下,获得 $wholeAs?$decimal并不依赖于另一个。(你只需要独立获取3个输出中的1个。)我展示了 $wholeAsFloat$wholeAsInt,因为 floor()返回一个浮点类型的数字,即使它返回的数字总是整数。(如果要将结果传递到类型提示的函数参数中,这一点很重要。)

我希望将一个浮点数小时/分钟,例如96.25,分别拆分为一个 DateInterval实例的小时和分钟,即96小时15分钟。我是这样做的:

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

在我的案子里,我不在乎第二次。

如果你可以指望它总是有2个小数位,你可以只使用字符串操作:

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

不知道性能,但对于我的简单的情况下,这是更好的..。

只是一个新的简单的解决方案,对于那些想要 将 Integer 部分和 Decimal 部分分割为两个整数分隔值的人:

5.25 -> Int part: 5; Decimal part: < strong > 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.

但是,小心! 现在从 -5.25得到: 整数部分: -5; 十进制部分: -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;

最后,用于打印小数点后2位的价格的附加片段:

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

这样你就不会得到5.5而不是5.05

试试这样... 这样比较容易

$var = "0.98";


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


$whole_no = $var-$decimal;


echo $whole_no;


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

你也可以使用这样的东西:

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

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

Code: (演示)

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

产出:

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

或者你可以分别给这两个变量赋值:

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

当将小时的十进制表达式转换为小时和分钟时,将小数部分转换为浮点形式特别有用。(演示)

$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";
}

产出:

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