Question

In the PHP manual for function sprintf I find:

e - the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less).

Now I'm on PHP 5.4, but would like to format a number with scientific notation and indicate the number of significant digits instead of decimal places. Is there a function for this, or should I write it myself?

Was it helpful?

Solution

I looked over the (one less) part, that's all I needed. thanks for the help!

This means that I just had to decrease the value of the precision by one to get my function to behave the way I wanted.

See here for the function I came up with tot support scientific notation using sprintf

    function sn($number,$precision = 1,$case = 'e'){ //scientific notation
      $precision--; 
      $string = str_replace('+','',sprintf('%.'.$precision.$case, $number))." ";
      return $string;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top