如何删除浮点数小数部分末尾的零?

I have in database product prices with 4 digits in decimal part, i.e;

4.5000
0.0050
5.0000

I want to show on the website these prices with minimum 2 decimal digits (without rounding), i.e;

4.50
0.005
5.00

I tried number_format, but it still leaves 4 digits in decimal part.

And also I need to use thousands separator on a base part of that number and own delimiter of decimal part.

How to do this?

function trimDecimalZero($num, $delim = ',', $tsep = ' ') {
    @list($base, $decimals) = explode('.',
        rtrim(number_format((float) $num, 4, '.', $tsep), '0'));

    if (intval($decimals)) {
        return sprintf('%s%s%s',
            $base, $delim, strlen($decimals) < 2 ? $decimals .'0' : $decimals);
    }

    return sprintf('%s%s%02d', $base, $delim, $decimals);
}

$nums = [4.5000, 0.0050, 5.0000];
foreach ($nums as $num) {
    var_dump(trimDecimalZero($num));
}

Result as expected;

string(4) "4,50"
string(5) "0,005"
string(4) "5,00"

For future reference, once again I modified my answer and yes, it outputs expected results:

function trimDecimalZero($number) {
    $number += 0;
    if(strlen(substr(strrchr($number, "."),1)) < 2) {
        $number = sprintf("%0.2f", $number);  
    }
    return $number;
}

echo trimDecimalZero(4.5000); //4.50
echo trimDecimalZero(0.00050); //0.00005
echo trimDecimalZero(5.0000); //5.00

Hope this would help future readers!

Try with -

echo number_format('4.5000', 2);

Update

$v = (float)'0.0050';
$newV = explode('.', $v);
if(strlen($newV[1]) < 2) {
   $v = number_format($v, 2);   
}
echo $v;

I guess that if it is for showing purposes, you could do something like this:

$number= 4.500000;
$number.= 'x';
$length = strlen($number);

$new_number = rtrim($number, '0x');
if (strlen($new_number) < $length) {
    $new_number .= '0';
}

echo $new_number;