Question

This:

function output($x) {
    return (float)($x==0 ? '0' : rtrim(($x<0 ? rtrim(sprintf('%.8F', $x), "0") : $x), "."));
}

    echo output(0.00008659);

Outputs:

8.659E-5

I want it to output this:

0.00008659

The reason why I'm using all those rtrim's and if's is because I want to remove trailing zeros when the number has less than 8 decimal places. For example, 1.0000 should output 1, 1.02000 should output 1.02, etc...

Was it helpful?

Solution

try this

function output($x) {
 $f = sprintf('%0.08f', $x);
 $f = rtrim($f,'0');
 $f = rtrim($f,'.');
 return $f;
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top