php 是否有一个解析字符串中指数的函数(例如:“9.5367431640625e-05”)或者是否必须通过正则表达式等手动完成?

发布于 2024-12-02 06:12:25 字数 409 浏览 4 评论 0原文

php是否有一个函数可以转换字符串(数据类型将是字符串,因为它是从外部源解析的),例如:

string(..) "9.5367431640625e-05"

并对所述字符串执行指数计算并返回计算值?

或者我是否需要使用 regexp 等风格的方式来执行以下操作:

9.5367431640625e-05 -> 9.5367431640625^-5 = calculated value

如果 php 没有这样的功能,那么如何实现此功能的最佳想法?我只是想进行模式匹配之类的东西,有

/^([0-9\.\-]+)e([0-9\.\-]+)$/

更好的想法吗?

谢谢!

does php have a function that will convert a string (data type will be string since its parsed from an external source) like:

string(..) "9.5367431640625e-05"

and perform the exponent calculations on said string and return the calculated value?

or do i need to use regexp etc style way to do something like:

9.5367431640625e-05 -> 9.5367431640625^-5 = calculated value

if php does not have such a function, best ideas on how to accomplish this? i was just going to pattern match something like

/^([0-9\.\-]+)e([0-9\.\-]+)$/

any better ideas?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七色彩虹 2024-12-09 06:12:25

您可以进行简单的转换:

$float = (float)$string;

但是除了检查 0 (这可能是一个有效值......)之外,无法进行错误检查。您还可以使用 floatval() 函数来执行相同的操作。

但通常您不必这样做,因为 PHP 会根据需要进行转换。请注意文档的该章:http://de.php。 net/manual/en/language.types.type-juggling.php

You could do a simple cast:

$float = (float)$string;

But besides checking for 0 (which might be a valid value ...) there is no way for error checking. You could also use the floatval() function which does the same.

But usually you won't have to do it as PHP will cast as needed. Mind that chapter of the documentation: http://de.php.net/manual/en/language.types.type-juggling.php

许你一世情深 2024-12-09 06:12:25

进行演员表:

$float  = (float) $string;

Do a cast:

$float  = (float) $string;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文