Convert float to plain string representation

后端 未结 4 1509
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 07:24

SO,

The problem

My question is about trivial thing: how to convert numeric string to it\'s plain (\"native\") representation. That means: if

相关标签:
4条回答
  • 2020-12-31 07:34

    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
    
    0 讨论(0)
  • 2020-12-31 07:45
    # convert output of used php-math functions like sin in scientific notation to decimal notation
    function xpnd($scientific, $precision){ # expand from scientific notation
      if(is_int($scientific)){ #don't convert integers
        return $scientific; 
      }
      return sprintf("%.".$precision."F", $scientific);
    }
    

    Where $precision is the desired number of fractional digits.

    0 讨论(0)
  • 2020-12-31 07:46

    (Updated to use non-depreciated functions as suggested by andufo; I chose explode, but you could use preg_split if you wanted. As a side note if anyone still reads down this far, the accepted answer fails - try it with my first and last test case.)

    I dug up a little gem from the PHP boards posted by benjcarson in 2002 who noted your exact problem with bcmath and scientific notation

    It needed some adjustment (his function didn't set the right scale, and failed on regular decimals, and as pointed out it did not account for the length of decimal places in the scale)

    function exp2int($exp) {
      list($mantissa, $exponent) = explode("e", strtolower($exp));
      if($exponent=='') return $exp;
      list($int, $dec) = explode(".", $mantissa);
      bcscale (abs($exponent-strlen($dec)));
      return bcmul($mantissa, bcpow("10", $exponent));
    }
    

    As a side note, your original code fails on any numbers smaller than 1E-40

    (As do all the current answers using sprintf)

    It would have been easier to debug if you posted more of your test cases, but this works for everything you've posted so far

    Test cases:

    echo exp2int("-1.82235135978667123456789E5"); \\-182235.135978667123456789
    echo exp2int("1.1350865232E-60"); \\0.0000000000000000000000000000000000000000000000000000000000011350865232
    echo exp2int("-15.482E-2"); \\-0.15482
    echo exp2int("1.5"); \\1.5
    echo exp2int("3"); \\3
    echo exp2int("123.123e10"); \\1231230000000.000 - you mentioned trailing 0's aren't a problem
    echo exp2int("123.123e-10"); \\0.0000000123123
    echo exp2int("123456789E-9"); \\0.123456789
    echo exp2int("12345.6789E-5"); \\0.123456789
    echo exp2int("1E-300"); \\0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
    
    0 讨论(0)
  • 2020-12-31 08:00

    For those who just wants to convert float to string.

    function float_to_string($float)
    {
        $string = (string)$float;
    
        if (preg_match('~\.(\d+)E([+-])?(\d+)~', $string, $matches)) {
            $decimals = $matches[2] === '-' ? strlen($matches[1]) + $matches[3] : 0;
            $string = number_format($float, $decimals,'.','');
        }
    
        return $string;
    }
    
    $float = 0.00000000020001;
    
    echo $float; // 2.0001E-10
    echo PHP_EOL;
    echo float_to_string($float); // 0.00000000020001
    echo PHP_EOL;
    
    $float = 10000000000000000000000;
    
    echo $float; // 1.0E+22
    echo PHP_EOL;
    echo float_to_string($float); // 10000000000000000000000
    echo PHP_EOL;
    
    0 讨论(0)
提交回复
热议问题