当前位置:网站首页>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
边栏推荐
- 实现接口 Interface Iterable&lt;T&gt;
- ORACLE 11G利用 ORDS+pljson来实现json_table 效果
- ORACLE EBS DATAGUARD 搭建
- Oracle EBs and apex integrated login and principle analysis
- CSRF attack
- ERNIE1.0 与 ERNIE2.0 论文解读
- Cognitive science popularization of middle-aged people
- 【Ranking】Pre-trained Language Model based Ranking in Baidu Search
- Typeerror in allenlp: object of type tensor is not JSON serializable error
- Oracle EBS database monitoring -zabbix+zabbix-agent2+orabbix
猜你喜欢
随机推荐
第一个快应用(quickapp)demo
[medical] participants to medical ontologies: Content Selection for Clinical Abstract Summarization
ssm+mysql实现进销存系统
Implement interface Iterable & lt; T&gt;
【信息检索导论】第二章 词项词典与倒排记录表
oracle-外币记账时总账余额表gl_balance变化(上)
一个中年程序员学习中国近代史的小结
Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix
Network security -- intrusion detection of emergency response
[introduction to information retrieval] Chapter 3 fault tolerant retrieval
[model distillation] tinybert: distilling Bert for natural language understanding
【模型蒸馏】TinyBERT: Distilling BERT for Natural Language Understanding
实现接口 Interface Iterable&lt;T&gt;
使用Matlab实现:Jacobi、Gauss-Seidel迭代
Oracle EBS database monitoring -zabbix+zabbix-agent2+orabbix
Data warehouse model fact table model design
JSP intelligent community property management system
MapReduce concepts and cases (Shang Silicon Valley Learning Notes)
Implementation of purchase, sales and inventory system with ssm+mysql
spark sql任务性能优化(基础)