如何在PHP中将Exponentials转换为小数 [英] How to convert Exponentials to Decimals in PHP

查看:193
本文介绍了如何在PHP中将Exponentials转换为小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个字符串:

  9.018E-14 

现在我想将其转换为正常的十进制数字。

解决方案

编辑:这里有一些PHP的魔力:

  $ stringval =12e-3; 
$ numericval = 0 + $ stringval;




如果字符串不包含任何字符'。','e'或'E',并且数值适合整数类型限制(由PHP_INT_MAX定义),则字符串将被评估为整数。如果您需要更灵活的格式(例如从相同的数字中提取四个数字)字符串),使用 sscanf 像这样:

  $ stringval =12e -3\" ; 
$ numericval = sscanf($ stringval,%f)[0];
echo $ numericval;


I have a string like this:

  9.018E-14

Now I want to convert to this to the normal decimal numbers.

解决方案

EDIT: Here is some PHP magic:

$stringval = "12e-3";
$numericval = 0 + $stringval;

From the PHP docs:

If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

If you need a more flexible format (e.g. extract four numbers from the same string), use sscanf like this:

$stringval = "12e-3";
$numericval = sscanf($stringval, "%f")[0];
echo $numericval;

这篇关于如何在PHP中将Exponentials转换为小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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