1

运行以下命令,我希望收到N, Y, Y.

我理解为什么我不这样做,因为'0.00' != '0'对于第二个示例,但是否有一致的测试方法,0无需强制转换为 float/double,也无需=====.

echo bcmul( '5.1', '2.234', 2 );
echo bcmul( '5.1', '2.234', 2 ) === '0' ? '  Y  ' : '  N  ';
echo "<br/>";

echo bcmul( '0.00', '000.00', 2 );
echo bcmul( '0.00', '000.00', 2 ) === '0' ? '  Y  ' : '  N  ';
echo "<br/>";

echo bcmul( '0', '0', 2 );
echo bcmul( '0', '0', 2 ) === '0' ? '  Y  ' : '  N  ';
echo "<br/>";

笔记

为什么我不想放弃===

如果我通过诸如 之类的方法将功能作为更大项目的一部分提供get_total_cost(),我认为其他开发人员在期望函数返回数值时不得不放弃严格比较是不直观的作为一个字符串。

4

2 回答 2

2

A. Yes0.00 !== 0是有效的,因为它们不是同一类型

var_dump(0.00,0);

输出

float 0
int 0

B. 0 !== "0" is valid because they are not the same type

var_dump(0,"0");

Output

int 0
string '0' (length=1)

C. Why don't I want to drop the ===

var_dump("hello" == 0 );  true
var_dump("hello" === 0 );  false

Conclusion

$a === $b TRUE if $a is equal to $b, and they are of the same type.

I guess this is what you want

echo (int) bcmul('0.00', '000.00', 2) === (int) '0' ? '  Y  ' : '  N  '; 
       ^                                    ^
于 2013-02-12T12:34:26.027 回答
0

You could try using ctype_digit() to determine if the returned string contains a "clean" int, or if it contains a floating point dot somewhere, and then just have two sets of Y/N functions; One for string-ints and one for string-floats.

于 2013-02-12T12:39:39.180 回答