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

i want to covert this number 23333333to 23 only with php function and if it be 20 i want to convert it to 2

原文地址: http://www.cveoy.top/t/topic/gypb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录