php 将显示为字符串的指数数转换为十进制数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5340283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert exponential number presented as string to a decimal
提问by zerkms
Let's suppose I have a stringthat contains "7.2769482308e+01" (this number came from 3rd party software, I cannot control the format).
假设我有一个包含“7.2769482308e+01”的字符串(这个数字来自 3rd 方软件,我无法控制格式)。
What is the cheapest way to convert it into decimal 72.769482308
?
将其转换为十进制最便宜的方法是什么72.769482308
?
The only solution I can think of is to split decimal + exponential part and use multiplication. But may be there some built it function to do the same?
我能想到的唯一解决方案是拆分小数+指数部分并使用乘法。但是可能有一些内置的功能来做同样的事情吗?
NOTE: Guys, yes, I've read Convert exponential to a whole number in PHPand Convert exponential number to decimal in php. And that questions are irrelevant, since they already have a number, but I have a string.
注:伙计们,是的,我读过转换指数在PHP中的整数和在PHP转换指数数为十进制。这些问题无关紧要,因为它们已经有了一个数字,但我有一个string。
回答by hsz
What about a simple cast to a float value ?
简单地转换为浮点值怎么样?
$string = "7.2769482308e+01";
$float = (float) $string;
回答by jwhat
I had success using number_format(1.2378147769392E+14, 0, '', '')
which was originally provided by this question.
我成功使用了number_format(1.2378147769392E+14, 0, '', '')
最初由this question提供的内容。
This also works when the value is a string, like so: number_format("1.2378147769392E+14", 0, '', '')
. Go ahead, give it a try.
这也适用,如果值是一个字符串,像这样:number_format("1.2378147769392E+14", 0, '', '')
。来吧,试一试。
回答by steloh
Here is a very cheap one: $float = "7.2769482308e+01" + 0;
这是一个非常便宜的: $float = "7.2769482308e+01" + 0;