Search code examples
phpnumber-formatting

Remove trailing zeros from decimal string


I have these numbers:

0.00000100
0.00100000
0.01000000
0.00001000

and I want to remove unnecessary zero in decimal by using this :

$decimal_cutter = (float) $decimal_cutter;
echo sprintf('%f', $decimal_cutter) . '<br>';

and it works for some numbers, some others produce this :

1.0E-6
0.001
0.01
1.0E-5

I want to use decimal, instead of scientific format.

Please note, I tried to use number_format() also, but keep in mind that by setting number of decimal points can cut the rest of numbers. I just want to remove the 0 after 1.


Solution

  • Since you mention your data is always "1", here's a simple way:

    $s = '0.00000100';
    echo strstr($s, '1', true).'1';
    

    Note: make sure you convert your data to string first