• php 常用助手函数


     1 <?php
     2 
     3 if (!function_exists('bcSum')) {
     4     function bcSum($scale, ...$args): string
     5     {
     6         $result = '0.00';
     7         foreach ($args as $arg) {
     8             $result = bcadd($result, $arg, $scale);
     9         }
    10         return $result;
    11     }
    12 }
    13 
    14 if (!function_exists('yuanToWan')) {
    15     function yuanToWan($yuan): float
    16     {
    17         return floatval(bcdiv($yuan, 10000, 6));
    18     }
    19 }
    20 
    21 
    22 if (!function_exists('calCompleteDegree')) {
    23     function calCompleteDegree($actual, $target, $scale = 4): float|null
    24     {
    25         if (bccomp($target, '0.00', $scale + 1) === 0) {
    26             return null;
    27         } elseif (bccomp($target, '0.00', $scale + 1) > 0) {
    28             return round(bcdiv($actual, $target, $scale + 1), $scale);
    29         } else {
    30             return round(
    31                 bcdiv(
    32                     bcsub(bcmul(2, $target, $scale + 1), $actual, $scale + 1),
    33                     $target,
    34                     $scale + 1
    35                 ),
    36                 $scale
    37             );
    38         }
    39     }
    40 }
    41 
    42 if (!function_exists('calPercentage')) {
    43     function calPercentage($num1, $num2, $scale = 4): float|string
    44     {
    45         return bccomp($num2, '0.00', $scale + 1) !== 0
    46             ? round(bcdiv($num1, $num2, $scale + 1), $scale) : '';
    47     }
    48 }
    49 
    50 if (!function_exists('arrayKsort')) {
    51     function arrayKsort(&$array): bool
    52     {
    53         if (!isset($array) || !is_array($array)) {
    54             return false;
    55         }
    56 
    57         foreach ($array as $k => $v) {
    58             unset($array[$k]);
    59             $key = mb_convert_encoding($k, 'GBK', 'UTF-8');
    60             $array[$key] = $v;
    61         }
    62         ksort($array);
    63         foreach ($array as $k => $v) {
    64             unset($array[$k]);
    65             $key = mb_convert_encoding($k, 'UTF-8', 'GBK');
    66             $array[$key] = $v;
    67         }
    68         return true;
    69     }
    70 }
  • 相关阅读:
    spring boot redis 使用代码配置 redis的连接信息
    mysql Last_SQL_Error
    mysql relay log
    idea插件开发
    全网唯一正常能用的centos7 安装mysql5.7.35 22 33 25
    LVM逻辑卷管理器
    git提交代码
    数据中心统一网络实战:FCoE部署指南
    详解HBA、NIC与CNA的区别
    存储网络 – 了解FCoE的八个技术细节
  • 原文地址:https://www.cnblogs.com/gaogaoxingxing/p/16202845.html
Copyright © 2020-2023  润新知