将数字字符串转换为浮点型数据 [英] Cast a numeric string as float type data

查看:59
本文介绍了将数字字符串转换为浮点型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行类似于 intval() 但用于小数的 PHP 命令是什么?

What is the PHP command that does something similar to intval(), but for decimals?

例如.我有字符串33.66"我想在将其发送到 MSSQL 之前将其转换为十进制值.

Eg. I have string "33.66" and I want to convert it to decimal value before sending it to MSSQL.

推荐答案

floatval()?

$f = floatval("33.66");

通过使用强制转换而不是函数调用,您可以将类型转换的时间缩短几纳秒.但这属于微优化领域,所以不要担心,除非您每秒执行数百万次此类操作.

You can shave a few nanoseconds off of type conversions by using casting instead of a function call. But this is in the realm of micro-optimization, so don't worry about it unless you do millions of these operations per second.

$f = (float) "33.66";

我还建议学习如何使用 sscanf() 因为有时它是最方便的解决方案.

I also recommend learning how to use sscanf() because sometimes it's the most convenient solution.

list($f) = sscanf("33.66", "%f");

这篇关于将数字字符串转换为浮点型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆