Remove Useless Zero Digits from Decimals in PHP

Remove useless zero digits from decimals in PHP

$num + 0 does the trick.

echo 125.00 + 0; // 125
echo '125.00' + 0; // 125
echo 966.70 + 0; // 966.7

Internally, this is equivalent to casting to float with (float)$num or floatval($num) but I find it simpler.

Remove useless zero digits from decimals and remove decimals above 2 in PHP

There are probably better ways to achieve this, but the first approach that springs to mind is simply trimming it away using rtrim() with .0 as the trim. This will however also trim entirely away when you have cases like 0.00 (and just return an empty string), so we can perform an explicit check for that.

function myFormat($number, $precision = 2) {
$number = rtrim(round($number, $precision), '.0');
if (empty($number)) {
$number = 0;
}
return $number;
}

echo myFormat(0.00); // 0
echo myFormat(0.01); // 0.01
echo myFormat(0.10); // 0.1
echo myFormat(1.00); // 1
echo myFormat(2245.0090); // 2245.01

Remove useless zero digits from decimals

This should do it:

$number = rtrim($number, '0');

rtrim takes the character(s) to trim as a second (optional) argument.

If you want it to work also for integers (eg. 1000), try this:

if (false !== strpos($number, '.'))
$number = rtrim($number, '0');

Even better, to remove trailing dot in numbers like 0.00000:

if (false !== strpos($number, '.'))
$number = rtrim(rtrim($number, '0'), '.');

PHP how to keep the result in decimal format and remove unnecessary zero?

From comments:

$number = 0.000010950000;
$number = number_format($number, 10, '.',''); // to avoid scientific notation of tiny decimals
$number = rtrim((string)$number,'0');
$number = rtrim((string)$number,'.');

echo $number;

What this does:

the first rtrim will clear the excess 0, the second rtrim will clear any trailing . after the first rtrim is completed.

Why not combine the rtrims together (rtrim($number, '0.'))?

This will cause 10.0000 to become 1 which is obviously incorrect.

Examples:

$number = 0.000010950000;

output: $number = 0.00001095

and

$number = 10.000000;

output: $number = 10

number_format() php remove trailing zeros

You can add 0 to the formatted string. It will remove trailing zeros.

echo number_format(3.0, 1, ".", "") + 0; // 3

A Better Solution: The above solution fails to work for specific locales. So in that case, you can just type cast the number to float data type. Note: You might loose precision after type casting to float, bigger the number, more the chances of truncating the number.

echo (float) 3.0; // 3

Ultimate Solution: The only safe way is to use regex:

echo preg_replace("/\.?0+$/", "", 3.0); // 3
echo preg_replace("/\d+\.?\d*(\.?0+)/", "", 3.0); // 3

Snippet 1 DEMO

Snippet 2 DEMO

Snippet 3 DEMO

How to strip trailing zeros in PHP

Forget all the rtrims, and regular expressions, coordinates are floats and should be treated as floats, just prepend the variable with (float) to cast it from a string to a float:

$string = "37.422005000000000000000000000000";
echo (float)$string;

output:

37.422005

The actual result you have are floats but passed to you as strings due to the HTTP Protocol, it's good to turn them back into thier natural form to do calculations etc on.

Test case: http://codepad.org/TVb2Xyy3

Note: Regarding the comment about floating point precision in PHP, see this: https://stackoverflow.com/a/3726761/353790

Remove useless 0 after 3 digits and round after 6 digit

Use a combination of floor and sprintf to truncate the float to a string with 3 decimal places. Then use max to compare it with the rounded float. PHP will compare the values numerically, returning the first parameter (padded with zeros) if they are numerically the same, only returning the second value if it is numerically greater (ie there is a digit larger than 0 after the third decimal place).

$value = max(
sprintf("%.3f", floor($value * pow(10, 3)) / pow(10, 3)),
round($value, 6));

Removing all decimals in PHP

You can do it in PHP:

round($val, 0);

or in your MYSQL statement:

select round(foo_value, 0) value from foo

Any php function to to drop decimal part if its zero

I think this works that way by default in PHP. If you use proper number type like float or double.

If you're using string then you need to map

$a = '2.00';
echo (float)$a; // 2

Example of using float

$a = 2.00;
echo $a; //2

or

2.00 + 0; //2

If you want to format the number to show decimal part 2.00 you need to use number_format function (http://php.net/manual/en/function.number-format.php)

$a = 2.00
echo number_format($a, 2); // 2.00


Related Topics



Leave a reply



Submit