浮点到int类型在php中转换25634到25633

浮点到int类型在php中转换25634到25633,php,Php,是否有任何限制问题?它只在256个数之后出现意外。< P>使用浮点而不是int。你将得到与你的问题相同的期望结果。 $amount = (256.34 * 100); echo (int) ($dollor_amount); // out should be 25634 but this giving 25633, not sure why? $amount = (255.34 * 100); echo (int) ($dollor_amount); // For 255.34 it re

是否有任何限制问题?它只在256个数之后出现意外。

< P>使用浮点而不是int。你将得到与你的问题相同的期望结果。

$amount = (256.34 * 100);
echo (int) ($dollor_amount);

// out should be 25634 but this giving 25633, not sure why?


$amount = (255.34 * 100);
echo (int) ($dollor_amount);
// For 255.34 it returning fine output 25534

看见基本上,你永远不能100%依赖浮点数。这里最有可能的是
256.34*100
实际上是
256.33999999999999*100
,所以你得到了
25633.99999
,当键入int时,小数就被切掉了。我想知道的是为什么,它会打印预期的结果。这与浮点运算的执行方式有关(以及
(int)的事实)3.14
变为
3
,无论小数点有多接近上整数),这是不可依赖的。这可能会提供更多信息。在这种情况下,您也不能强制转换它。如果我们不转换为int,为什么要强制转换为float到float-?我需要int值
$amount = (256.34 * 100);
echo (float) ($amount);

// out should be 25634 but this giving 25633, not sure why?

$amount = (255.34 * 100);
echo (float) ($amount);