You can use the number_format function in PHP to achieve this. Here's an example:

$num = 2.3333333;

// Convert to 1 decimal place
$num = number_format($num, 1);

// Remove trailing zeros
$num = rtrim($num, '0');

// Remove decimal point if there are no decimal places
if (substr($num, -1) == '.') {
    $num = substr($num, 0, -1);
}

echo $num; // Output: 2.3

This code first uses the number_format function to convert the number to 1 decimal place. Then it uses rtrim to remove any trailing zeros. Finally, it checks if the number ends with a decimal point (indicating that there are no decimal places) and removes it if necessary

标签: 教育


原文地址: https://gggwd.com/t/topic/gypb 著作权归作者所有。请勿转载和采集!