Skip to content

Commit

Permalink
Merge pull request #54 from wonder-game/develop
Browse files Browse the repository at this point in the history
feat: 新增format_number函数
  • Loading branch information
linkunyuan authored Sep 19, 2023
2 parents 96a47ba + 1df0bbd commit c45030e
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,43 @@ function array_sort_multi($data = [], $field = '', $direction = SORT_DESC)
{
if ( ! $data) return [];
$arrsort = [];
foreach ($data as $uniqid => $row) {
foreach ($row as $key => $value) {
$arrsort[$key][$uniqid] = $value;
foreach ($data as $uniqid => &$row) {
foreach ($row as $key => &$value) {
$arrsort[$key][$uniqid] = $value = format_number($value, 2, true);
}
unset($value);
}
unset($row);
if ($direction) {
array_multisort($arrsort[$field], $direction, $data);
}
return $data;
}
}

if ( ! function_exists('format_number')) {
/**
* 格式化数字或百分比
* @param numeric|string $num 要处理的数字或百分比
* @param numeric $prec 小数点后的精度
* @param bool $multiply 如果是百分比是否要乘以100
* @return numeric|string
*/
function format_number($num, $prec = 2, $multiply = true)
{
$num = trim($num);
$percent = false;

// 百分比
if (preg_match('/[0-9.]+%$/', $num)) {
$percent = true;
$num = ($multiply ? 100 : 1) * (float)$num;
}
is_numeric($num) && $num = str_replace('.00', '', sprintf("%01.{$prec}f", $num));
return $num . ($percent ? '%' : '');
}
}

if ( ! function_exists('listdate')) {
/**
* 返回两个日期之间的具体日期或月份
Expand Down

0 comments on commit c45030e

Please sign in to comment.