将float转换为纯字符串表示形式 [英] Convert float to plain string representation

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

问题描述




$ b

我的问题是关于微不足道的事情:如何将数字字符串转换为纯(本地)表示形式。这意味着:如果数字字符串已经在普通视图中,请保持原样,但是如果是科学记数法,则将其转换。示例:

 
3 - >3
1.5 - >1.5
-15.482E-2 - >-0.15482

数字字符串应该是有效的,如果不是 - 那么它是而不是转换的情况(例如,我们可以自由返回null或空字符串)。
$ b

用例



这是 bcmath 所需要的,因为它不能用科学的浮点数工作。因此,他们需要转换为纯字符串(问) -Zero-E>这里)。这个用例的重要结果是,数字字符串可以是 1E-300 1E + 500 。因为我们正在处理 bcmath - 它的目的是要处理这样的事情。



我的方法



现在,我已经用 regex-almighty 的问题,如:

  function parseFloat($ string)
{
$ string =(string)$ string; $($ + $)$($) ,$ string,$ matches))
{
$ precision = false!==($ dot = strpos($ matches [1],'。'))
?strlen($ matches [1]) - $ dot-1
:0;
$ precision = $ matches [2] ==' - '
?$ precision +(int)$ matches [3]
:$ precision - (int)$ matches [3];
return number_format($ string,$ precision< 0?0:$ precision,'','');

if(preg_match('/ ^ [+ - ]?(\d + | \d + \.\d +)$ /',$ string))
{
返回$ string;






我觉得应该有更简单明智的方法来做到这一点。如何以更简单的方式在PHP中实现?可能是一些棘手的 sprintf() 格式? p>

重要说明:我不想处理精确度。我想要黑盒子。在那里传递数字 - 将字符串作为输出。就这样。不想处理别的事情。实际上,我所有的正则表达式都是关于计算长度和长度的。精度 - 所以,当然,如果明确地传递它们(例如参数) - 我们猫摆脱正则表达式。但是 - 不,那不是我想要的。

解决方案

因为 sprintf() with %。f1e-8等表达式中遇到问题,必须:

$ pre $函数convertFloat($ floatAsString)
{
$ norm = strval(floatval($ floatAsString)); ($ e = strrchr($ norm,'E'))=== false){
return $ norm;

if


return number_format($ norm,-intval(substr($ e,1)));



$ b $ p
$ b

测试结果:

  3 3 
1.5 1.5
-15.482e-2 -0.15482
1e-8 0.00000001
1e + 3 1000
-4.66E-2 -0.0466
3e-3 0.003


SO,

The problem

My question is about trivial thing: how to convert numeric string to it's plain ("native") representation. That means: if numeric string is already in plain view, leave it as it is, but if it is in scientific notation, convert it. Sample:

"3"          -->  "3"
"1.5"        -->  "1.5"
"-15.482E-2" -->  "-0.15482"

Numeric string supposed to be valid, and if it's not - then it's not a case for conversion (we're free to return null or empty string, for example).

Use-case

That is needed for bcmath because it can't work with scientific floats. Thus, they need to be converted to plain strings (asked here). So important consequence from this use-case is that numeric string can be something like 1E-300 or 1E+500. Since we're working with bcmath - it's intention is to handle such things.

My approach

For now, I've implemented that with , like:

function parseFloat($string)
{
   $string = (string)$string;
   if(preg_match('/^[+-]?(\d+|\d+\.\d*)[Ee]([+-]?)(\d+)$/', $string, $matches))
   {
      $precision = false!==($dot=strpos($matches[1], '.'))
                   ?strlen($matches[1])-$dot-1
                   :0;
      $precision = $matches[2]=='-'
                   ?$precision + (int)$matches[3]
                   :$precision - (int)$matches[3];
      return number_format($string, $precision<0?0:$precision, '', '');
   }
   if(preg_match('/^[+-]?(\d+|\d+\.\d+)$/', $string))
   {
      return $string;
   }
}

The question

I feel that there should be more simple and wise way to do that. How to achieve that in more simple way in PHP? May be some tricky sprintf() format?

Important note: I don't want to deal with precision. I want black box. Pass something numeric there - got string as output. That's all. Don't want to deal with anything else. In fact, all my regex are about calculating length & precision - so, sure, if pass them explicitly (as parameters, for example) - we cat get rid of regex. But - no, that's not what I want.

解决方案

Because sprintf() with "%.f" has trouble with expressions such as "1e-8", some text processing may be required:

function convertFloat($floatAsString)
{
    $norm = strval(floatval($floatAsString));

    if (($e = strrchr($norm, 'E')) === false) {
        return $norm;
    }

    return number_format($norm, -intval(substr($e, 1)));
}

Tested with:

3          3
1.5        1.5
-15.482e-2 -0.15482
1e-8       0.00000001
1e+3       1000
-4.66E-2   -0.0466
3e-3       0.003

这篇关于将float转换为纯字符串表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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