Scenario:
To trim leading zeros from positive and negative floating point numbers
Input:
000.123
00.123
01.123
-001.123
-00.123
-040.123
Desired output:
0.123
0.123
1.123
-1.123
-0.123
-40.123
Question:
Is there an inbuilt function which will make this specific formatting easier and more efficient than running each number through combinations of substr()
, strpos()
, explode()
and if
statements?
I guess your numbers are saved as a string, so in order to get your output you just simple cast them to a float
or double
like this:
echo (float) $number;
For more information about casting see the manual: http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting
Just cast it as float
Like this example:
<?php
$number = '-000.220';
echo (float)$number;
This way you remove all leading zeros, either being a positive or negative number