【问题标题】:how to set the min and max to 2 decimal places in php [duplicate]如何在php中将最小值和最大值设置为小数点后2位[重复]
【发布时间】:2024-01-17 15:31:01
【问题描述】:
<?php
$result = array_filter($_POST);    
if(!empty($result)){
echo ((max($result)+min($result))/2)*1.2 ."|".((max($result)+min($result))/2)*1.3;
}
?>

大家好..如何将上述代码的最小和最大结果设置为小数点后 2 位?实际上,处理完成后,结果将出现在输入类型文本中

【问题讨论】:

标签: php set max min


【解决方案1】:

在 PHP 中使用number_format 函数。

number_format ( float $number [, int $decimals = 0 ] ) : 字符串
欲了解更多信息,请参阅here

$yourNumber = 1235.343;

echo number_format ($yourNumber, 2);

// Will output 1,235.34 (with two decimals, specified in number_format as second paramter.

编辑: Max / Min 函数返回混合值。确保其float,然后将其传递给number_format。它将返回string

【讨论】: