• 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 }
  • 相关阅读:
    编写SASS代码
    表单
    动画和变形
    图片多媒体
    基本概念
    弹性布局
    HTML和CSS概述
    页面的制作过程
    盒子定位体系
    css盒子
  • 原文地址:https://www.cnblogs.com/gaogaoxingxing/p/16202845.html
Copyright © 2020-2023  润新知