PHP - 金额操作各种实战实例,总有你想要的

简单粗暴 ⛽️

金额格式转换千分符分隔

// 原金额
$money = 100000001.23;

// 格式化后的金额
$new_money = number_format($money,2,'.',','); // 100,000,001.23


金额校验函数

/**
 * 金额校验函数
 * @param $value
 * @param bool $isZero
 * @param bool $negative
 * @return bool
 */
function isAmount($value, $isZero=false, $negative=false){
    
    
    // 必须是整数或浮点数,且允许为负
    if (!preg_match("/^[-]?\d+(.\d{1,2})?$/", $value)){
    
    
        return false;
    }
    // 不为 0
    if (!$isZero && empty((int)($value*100))){
    
    
        return false;
    }
    // 不为负数
    if (!$negative && (int)($value * 100) < 0){
    
    
        return false;
    }
    return true;
}

猜你喜欢

转载自blog.csdn.net/qq_35453862/article/details/116661663