当前位置:网站首页>Conversion of numerical amount into capital figures in PHP
Conversion of numerical amount into capital figures in PHP
2022-07-02 07:28:00 【Snow wind in the night sky】
Preface
Sometimes it is necessary to convert the numerical amount to the amount in Chinese characters , Although there are many ways online , But most of them are chaotic , Here is one that is now in use 、 It feels good .
/** * Convert numerical amount into capital figures * @param [float] $num Numeric type * @return void */
function convert_case_number($num)
{
// Convert number types
$num = (float) $num;
// Judge $num Whether there is
if (!$num) {
return ' Zero circle ';
}
// Keep two decimal places
$num = sprintf("%.2f", $num);
// Convert floating point to integer
$tem_num = $num * 100;
// Judge the length of the number
$tem_num_len = strlen($tem_num);
if ($tem_num_len > 14) {
return ' The number is too big ';
}
// Capital numbers
$dint = array(' zero ', ' one ', ' Ii. ', ' 3 ', ' boss ', ' wu ', ' lu ', ' Retailer, ', ' ', ' nine ');
// Amount unit in words
$danwei = array(' Thousand ', ' Bai ', ' Ten ', ' Billion ', ' Thousand ', ' Bai ', ' Ten ', ' ten thousand ', ' Thousand ', ' Bai ', ' Ten ', ' round ');
$danwei1 = array(' horn ', ' branch ');
// Empty variables are used to save the conversion string
$daxie = '';
// Split numbers , Distinguish rounded corners
list($left_num, $right_num) = explode('.', $num);
// Calculate the unit length
$danwei_len = count($danwei);
// Calculate the length of the split string
$left_num_len = strlen($left_num);
$right_num_len = strlen($right_num);
// Cycle calculation of billions of yuan, etc
for ($i = 0; $i < $left_num_len; $i++) {
// Loop single text
$key_ = substr($left_num, $i, 1);
// The judgment number is not equal to 0 Or a number equal to 0 And the amount unit is 100 million 、 ten thousand 、 round , Just return the string of complete units
if ($key_ !== '0' || ($key_ == '0' && ($danwei[$danwei_len - $left_num_len + $i] == ' Billion ' || $danwei[$danwei_len - $left_num_len + $i] == ' ten thousand ' || $danwei[$danwei_len - $left_num_len + $i] == ' round '))) {
$daxie = $daxie . $dint[$key_] . $danwei[$danwei_len - $left_num_len + $i];
} else {
// Otherwise, the unit is not included
$daxie = $daxie . $dint[$key_];
}
}
// Circularly calculate angular minutes
for ($i = 0; $i < $right_num_len; $i++) {
$key_ = substr($right_num, $i, 1);
if ($key_ > 0) {
$daxie = $daxie . $dint[$key_] . $danwei1[$i];
}
}
// Calculate the converted length
$daxie_len = strlen($daxie);
// Set the text slice from 0 Start ,utf-8 Chinese characters account for 3 Characters
$j = 0;
while ($daxie_len > 0) {
// Slice two Chinese characters each time
$str = substr($daxie, $j, 6);
// Judge that the text after slicing is not equal to ten thousand 、 Zero circle 、 zero 、 zero zero
if ($str == ' zero ' || $str == ' Zero circle ' || $str == ' zero ' || $str == ' zero zero ') {
// Slice again
$left = substr($daxie, 0, $j);
$right = substr($daxie, $j + 3);
$daxie = $left . $right;
}
$j += 3;
$daxie_len -= 3;
}
return $daxie . ' whole ';
}
for example : A lowercase letter :172190.20 element convert to Capitalization : One hundred seventy-two thousand one hundred ninety yuan and twenty cents
边栏推荐
- Oracle 11g sysaux table space full processing and the difference between move and shrink
- ORACLE APEX 21.2安裝及一鍵部署
- 中年人的认知科普
- 【Torch】最简洁logging使用指南
- Explanation of suffix of Oracle EBS standard table
- [introduction to information retrieval] Chapter 7 scoring calculation in search system
- ORACLE EBS DATAGUARD 搭建
- SSM personnel management system
- Oracle rman半自动恢复脚本-restore阶段
- Oracle APEX 21.2 installation et déploiement en une seule touche
猜你喜欢
Feeling after reading "agile and tidy way: return to origin"
@Transational踩坑
离线数仓和bi开发的实践和思考
Ding Dong, here comes the redis om object mapping framework
架构设计三原则
【信息检索导论】第二章 词项词典与倒排记录表
SSM laboratory equipment management
Implementation of purchase, sales and inventory system with ssm+mysql
使用Matlab实现:Jacobi、Gauss-Seidel迭代
[model distillation] tinybert: distilling Bert for natural language understanding
随机推荐
Delete the contents under the specified folder in PHP
Illustration of etcd access in kubernetes
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
Oracle EBS ADI development steps
oracle-外币记账时总账余额表gl_balance变化(上)
Three principles of architecture design
sparksql数据倾斜那些事儿
JSP智能小区物业管理系统
【调参Tricks】WhiteningBERT: An Easy Unsupervised Sentence Embedding Approach
Data warehouse model fact table model design
Oracle apex Ajax process + dy verification
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
聊天中文语料库对比(附上各资源链接)
Explanation of suffix of Oracle EBS standard table
Oracle APEX 21.2 installation et déploiement en une seule touche
读《敏捷整洁之道:回归本源》后感
矩阵的Jordan分解实例
ssm超市订单管理系统
Practice and thinking of offline data warehouse and Bi development
Typeerror in allenlp: object of type tensor is not JSON serializable error