Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to easily convert digital amount to traditional Chinese capitalization by PHP

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

PHP how to easily convert the digital amount into traditional Chinese capitalization, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Digital amount is converted to traditional Chinese capitalization.

Well, I can't believe I'm writing this.

You can test it here. (https://sandbox.onlinephpfunctions.com/code/0cc068d3c89904e55d8aa9e55fdfa45eaab70c37)

Digital amount is converted to traditional Chinese capitalization:

If (! function_exists ('rmb_capital')) {/ * the amount is translated into Chinese capital * * @ param mixed $amount * @ return string * / function rmb_capital ($amount) {$capitalNumbers = [' zero', 'one','II','3', 'restaurant','Wu','Lu','Qi', 'Qing', 'Jiu' ] $integerUnits = ['', 'pick up', 'hundred', 'thousand',]; $placeUnits = ['', 'ten thousand', 'billion', 'trillion',]; $decimalUnits = ['angle', 'cent', 'per cent', 'millimeter',]; $result = []; $arr = explode ('.', $amount); $integer = trim ($arr [0]??','-') $decimal = $arr [1]??'; if (! ((int) $decimal)) {$decimal =';} / convert integer portion / / start from bit $integerNumbers = $integer? Array_reverse (str_split ($integer)): []; $last = null; foreach (array_chunk ($integerNumbers, 4) as $chunkKey = > $chunk) {if (! ((int) implode (', $chunk)) {/ / all 0 skip continue;} array_unshift ($result, $placeUnits [$chunkKey]) Foreach ($chunk as $key = > $number) {/ / remove duplicate zeros and the first zero, similar to: 1002 if (! $number & (! $last | | $key = = 0)) {$last = $number; continue;} $last = $number / / similar to 1022, the middle 0 is if ($number) {array_unshift ($result, $integerUnits [$key]);} array_unshift ($result, $capitalNumbers [$number]) }} if (! $result) {array_push ($result, $capitalNumbers [0]);} array_push ($result, 'circle'); if (! $decimal) {array_push ($result, 'integer');} / / convert $decimalNumbers = $decimal? Str_split ($decimal): []; foreach ($decimalNumbers as $key = > $number) {array_push ($result, $capitalNumbers [$number]); array_push ($result, $decimalUnits [$key]);} if (strpos ((string) $amount,'-') = = 0) {array_unshift ($result, 'negative');} return 'RMB'. Implode (', $result);}}

Function does not verify whether the parameter is a numeric value

A simple test:

: RMB zero round dsfa: RMB zero round round 0: RMB zero round round 1: RMB one round round 12: RMB 102yuan round 10: RMB 10102yuan round 101010: RMB 10102yuan round 1002: RMB 10102 round 1010: RMB 101010 round 1022: RMB 10202 round 100002: RMB 10002 round 1020345: RMB 1, 020, 000, 300, 000, 000,

Has been rewritten to an open source extension package: rmb-capital (https://packagist.org/packages/chuoke/rmb-capital)

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report