Echo number without scientific notation, while removing trailing zeros and decimal points

StackOverflow https://stackoverflow.com/questions/21391081

  •  03-10-2022
  •  | 
  •  

Pregunta

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...

¿Fue útil?

Solución

try this

function output($x) {
 $f = sprintf('%0.08f', $x);
 $f = rtrim($f,'0');
 $f = rtrim($f,'.');
 return $f;
} 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top