How to Strip Trailing Zeros in PHP

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

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

Remove trailing zeros from decimal string

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

Remove trailing zeros until 2 decimals in PHP

You can use float casting

echo (float) 11.2200;
echo "<br/>";
echo (float) 11.2000;
echo "<br/>";
echo (float) 11.2340;

and you have to check number of digits after decimal point and than get value like below :

$val=(float) 11.2000;
if(strlen(substr(strrchr($val, "."), 1))<2){
echo number_format($val,2);
}

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 remove trailing zero based on variable number

If I understood correctly, this should do what you need. Refer to the comments:

function get_formatted_number($number, array $options = []): string
{
// Default values
static $DEFAULT_DECIMALS = 2;
static $DEFAULT_REMOVE_TRAILING_ZEROS = false;
static $DEFAULT_TRAILING_ZEROS_DECIMALS = 0;

// Retrieve/sanitize options
$decimals = array_key_exists('decimals', $options)
? max($DEFAULT_DECIMALS, (int)$options['decimals'])
: $DEFAULT_DECIMALS;
$remove_trailing_zeros = array_key_exists('removeTrailingZero', $options)
? !!$options['removeTrailingZero']
: $DEFAULT_REMOVE_TRAILING_ZEROS;
$trailing_zeros_decimals = array_key_exists('minDecimalsIfZero', $options)
? max($DEFAULT_TRAILING_ZEROS_DECIMALS, (int)$options['minDecimalsIfZero'])
: $DEFAULT_TRAILING_ZEROS_DECIMALS;

// Set number of decimals from options
$formatted_number = number_format($number, $decimals);

// If option is set, remove trailing zeros, keeping an optional minimum
if ($remove_trailing_zeros) {
$formatted_number = (float)$formatted_number;
if ($trailing_zeros_decimals >= strlen(substr(strrchr($formatted_number, '.'), 1))) {
$formatted_number = number_format($formatted_number, $trailing_zeros_decimals);
}
}

return $formatted_number;
}

Demo here: https://3v4l.org/7eqBe

How to remove all leading zeroes in a string

ltrim:

$str = ltrim($str, '0');

Why rtrim remove trailing zeros?

Just do (without leading space before the left parenthesis). Rtrim strips ordinary spaces without defining it which seems to be the issue here. To be honest: I don't know why the rest is 12 and not 12:00 but without the space it works:

echo rtrim($str,"(+06)");

Are you only looking for the left part of the string and it's always the same number of characters, you could do this:

$str = substr($str,0,5);` 


Related Topics



Leave a reply



Submit