Free public algorithm to generate numbers in chinese-readable format

Works up to 8 digit figures. For figures more than 9 digits, we haven't implement “亿” and ”兆“ 

I happen to be the creator behind payment gateway Sapphiro Payment (www.sapphiro.com/payment).

Recently after a visit to mainland China (ShenZhen, then to Xiamen) via Hong Kong, I further upgraded my payment gateway to have a similar voice speech system whereby a bot voice (but doesn't sounds like a bot) will automatically say out loud a short announcement the payment amount received whenever a transaction takes place on WeChat or AliPay in China.

That's when I realised, that existing codes for the bot systems do not read numbers naturally like real numbers, whereas it would for the numbers when reading in English. (e.g. X thousands and Y hundreds etc). When the codes reads numbers in chinese, they just read it number by number. (E.g. 13,555,000 would be "speeched" out as 一三五五五零零零 [yi san wu wu wu ling ling ling])

Hence creating an algorithm to read those numbers in the correct, natural way chinese people read them was what I wanted to do and so it's been done, I'll leave the algorithm below.

Free for use by everyone. (P.S. Do take note that there are a few ways to read numbers in chinese format, my algorithm follows Singapore Mandarin and possibly mainland Chinese way of reading formats where 10,000 would be 一万 [yi wan] rather than 十千 and 1,300 would be read as 一千三百 rather than the Taiwanese way of reading it as 十三百。

 

Algorithm below: (Feel free to use it, feel free to improve it as well)

==================================================

$payment_amount = 14834628;
$chinese_readable_payment_amount = $payment_amount;

 

if(strlen($chinese_readable_payment_amount) >= 9)
{
    $chinese_readable_payment_amount = "大数目";
}
else
{
    $final_chinese_readable_payment_amount = "";

    if(strlen($chinese_readable_payment_amount) >= 5)
    {
        $has_back_numbers = true;
        $back_numbers = substr($chinese_readable_payment_amount, -4);
        $front_numbers = substr($chinese_readable_payment_amount, 0, strlen($chinese_readable_payment_amount) - 4);
    }
    else
    {
        $has_back_numbers = false;
    }
                
    if(strlen($front_numbers) == 4)
    {
        $final_chinese_readable_payment_amount.= substr($front_numbers, 0, 1)."千";
        $front_numbers = substr($front_numbers, 1);
    }
                    
    if(strlen($front_numbers) == 3)
    {
        $final_chinese_readable_payment_amount.= substr($front_numbers, 0, 1)."白";
        $front_numbers = substr($front_numbers, 1);
    }
                    
    if(strlen($front_numbers) == 2)
    {
        $final_chinese_readable_payment_amount.= substr($front_numbers, 0, 1)."十";
        $front_numbers = substr($front_numbers, 1);    
    }
                    
    $final_chinese_readable_payment_amount = $final_chinese_readable_payment_amount.$front_numbers;
                
    if($has_back_numbers)
    {
        $final_chinese_readable_payment_amount.= "万 ";

        if(strlen($back_numbers) == 4)
        {
            $final_chinese_readable_payment_amount.= substr($back_numbers, 0, 1)."千";
            $back_numbers = substr($back_numbers, 1);
        }
                        
        if(strlen($back_numbers) == 3)
        {
            $final_chinese_readable_payment_amount.= substr($back_numbers, 0, 1)."白";
            $back_numbers = substr($back_numbers, 1);
        }

        if(strlen($back_numbers) == 2)
        {
            $final_chinese_readable_payment_amount.= substr($back_numbers, 0, 1)."十";
            $back_numbers = substr($back_numbers, 1);
        }

        $final_chinese_readable_payment_amount = $final_chinese_readable_payment_amount.$back_numbers;
    }
}

echo number_format($payment_amount)."".$final_chinese_readable_payment_amount;

// The number 14834628 (14,834,628) will be displayed as 1千4百8十3万 4千6百2十8

?>

==================================================

Algorithm above brought to you by my Sapphiro Technology as part of Sapphiro Payment's upgrade. The algorithm above is free to use, and free for modifications by the public.

If you have your own startup idea, but cannot code, or looking for further upgrades to your web app, feel free to drop an e-mail with your requirements to [email protected] for a quotation that likely beats typical company quotations since I'm doing my startup work officeless without a physical location etc so no rent costs and none of those extra expenses that goes into the paying customer.

* The reason the algorithm skips figures bigger than 9 digits is because in chinese numbers, after 九千万 9000,0000 (or 90,000,000), a digit figure bigger than that would be 一亿 (一万万) and the next big number would be  一兆。But those two formats are confusing once it comes to using "一兆" because mainland China and Taiwan etc use those words for different digit figures. 一兆 can mean either 一万亿 or 一亿亿 depending on which han chinese territory you are in, so for now for anything with that kind of figure the algorithm will just output as  "Large number" written in chinese.

 

 

960 unique views
Chen Chun-You Felix Tan, based in Singapore
Chen Chun-You is the founder and creator of Altairo.com. Being a technopreneur himself, he writes some articles for fun during his free time.
<< Back
Forgot your password?
<< Back
Your password reset mail has been sent to you.