SoFunction
Updated on 2025-03-08

PHP converts numbers into RMB capital function sharing


/**
* RMB lowercase to uppercase
 *
* @param string $number value
* @param string $int_unit Currency unit, default "yuan", some requirements may be "circle"
* @param bool $is_round Whether to round the decimals
* @param bool $is_extra_zero Whether to end with 0 for integer part, and attach 0 to the number of decimals, such as 1960.30
 * @return string
 */
function rmb_format($money = 0, $int_unit = 'meta', $is_round = true, $is_extra_zero = false) {
// Divide the number into two segments
    $parts = explode ( '.', $money, 2 );
    $int = isset ( $parts [0] ) ? strval ( $parts [0] ) : '0';
    $dec = isset ( $parts [1] ) ? strval ( $parts [1] ) : '';
 
// If the decimal point is more than 2 digits, it will be cut directly without rounding it, otherwise it will be processed
    $dec_len = strlen ( $dec );
    if (isset ( $parts [1] ) && $dec_len > 2) {
        $dec = $is_round ? substr ( strrchr ( strval ( round ( floatval ( "0." . $dec ), 2 ) ), '.' ), 1 ) : substr ( $parts [1], 0, 2 );
    }
 
// When number is 0.001, the amount after the decimal point is 0 yuan
    if (empty ( $int ) && empty ( $dec )) {
return 'zero';
    }
 
// Definition
$chs = array ('0', '1', '2', '3', 'si', 'wu', 'lu', '7', 'ba', 'nine');
$uni = array ('', 'she', 'bai', 'qian');
$dec_uni = array ('angle', 'point');
$exp = array ('', '10,000' );
    $res = '';
 
// Find the integer part from right to left
    for($i = strlen ( $int ) - 1, $k = 0; $i >= 0; $k ++) {
        $str = '';
// According to Chinese reading and writing habits, every 4 words are converted into a paragraph, and i is constantly decreasing
        for($j = 0; $j < 4 && $i >= 0; $j ++, $i --) {
$u = $int {$i} > 0 ? $uni [$j] : ''; // Add units after non-0 numbers
            $str = $chs [$int {$i}] . $u . $str;
        }
$str = rtrim ( $str, '0' ); // Remove the end 0
$str = preg_replace ( "/0+/", "zero", $str); // Replace multiple consecutive 0s
        if (! isset ( $exp [$k] )) {
$exp [$k] = $exp [$k - 2] . 'billion'; // Construction unit
        }
        $u2 = $str != '' ? $exp [$k] : '';
        $res = $str . $u2 . $res;
    }
 
// If the decimal part is 00 after processing, it needs to be processed
    $dec = rtrim ( $dec, '0' );
    var_dump ( $dec );
// Find the decimal part from left to right
    if (! empty ( $dec )) {
        $res .= $int_unit;
 
// Whether to append 0 to numbers ending with 0 in integer part? Some systems have this requirement
        if ($is_extra_zero) {
            if (substr ( $int, - 1 ) === '0') {
$res .= 'zero';
            }
        }
 
        for($i = 0, $cnt = strlen ( $dec ); $i < $cnt; $i ++) {                 
$u = $dec {$i} > 0 ? $dec_uni [$i] : ''; // Add units after non-0 numbers
            $res .= $chs [$dec {$i}] . $u;
            if ($cnt == 1)
$res .= 'whole';
        }
 
$res = rtrim ( $res, '0' ); // Remove the end 0
$res = preg_replace ( "/0+/", "zero", $res ); // Replace multiple consecutive 0s
    } else {
$res .= $int_unit . 'whole';
    }
    return $res;
}