简体   繁体   English

如何自动将十进制转换为数字而不是PHP中的字符串?

[英]How to automatically convert decimal into number not a string in php?

How to make auto convert a decimal data type into number not a string in php? 如何使自动将十进制数据类型转换为数字而不是PHP中的字符串?

I changed data field into decimal(4,2) from double. 我将数据字段从double更改为十进制(4,2)。 I have many checks like 我有很多支票,例如

if ($row->field)

And $row->field === 0.00. 和$ row-> field === 0.00。 When it was double it's ok. 当它翻倍的时候还可以。 But when I get decimal in php it's get string type and '0.00' it's TRUE. 但是,当我在php中获取小数时,它会得到字符串类型和“ 0.00”,这是真的。

"Add (float) before $row->field" are not my way, because I change many field and too many places where I use it. “我在$ row-> field之前添加(浮点数)”不是我的方法,因为我更改了很多字段,并且在使用它的地方太多了。

I want find a better way, than change too many lines of code. 我想要找到一种比更改太多行代码更好的方法。 Maybe some extensions or something else. 也许一些扩展或其他。

Why not return a boolean expression from MySQL itself as part of your query? 为什么不从MySQL本身返回布尔表达式作为查询的一部分?

SELECT decimal_column <> 0.00 as bool_expression FROM table;

Then test the expression like: if($row->bool_expression) 然后测试表达式: if($row->bool_expression)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM