当前位置:网站首页>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
边栏推荐
- @Transational踩坑
- A slide with two tables will help you quickly understand the target detection
- 【Ranking】Pre-trained Language Model based Ranking in Baidu Search
- Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
- view的绘制机制(一)
- Oracle RMAN automatic recovery script (migration of production data to test)
- 使用MAME32K进行联机游戏
- allennlp 中的TypeError: Object of type Tensor is not JSON serializable错误
- CRP实施方法论
- Oracle rman半自动恢复脚本-restore阶段
猜你喜欢

Using compose to realize visible scrollbar

SSM personnel management system

Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix

view的绘制机制(一)

软件开发模式之敏捷开发(scrum)

Drawing mechanism of view (I)

点云数据理解(PointNet实现第3步)

【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
![[introduction to information retrieval] Chapter 6 term weight and vector space model](/img/42/bc54da40a878198118648291e2e762.png)
[introduction to information retrieval] Chapter 6 term weight and vector space model

自然辩证辨析题整理
随机推荐
JSP intelligent community property management system
[paper introduction] r-drop: regulated dropout for neural networks
Point cloud data understanding (step 3 of pointnet Implementation)
Oracle EBS DataGuard setup
【Ranking】Pre-trained Language Model based Ranking in Baidu Search
SSM second hand trading website
view的绘制机制(一)
view的绘制机制(三)
Using MATLAB to realize: power method, inverse power method (origin displacement)
Use matlab to realize: chord cut method, dichotomy, CG method, find zero point and solve equation
Ding Dong, here comes the redis om object mapping framework
Practice and thinking of offline data warehouse and Bi development
CONDA creates, replicates, and shares virtual environments
ssm超市订单管理系统
ARP attack
Alpha Beta Pruning in Adversarial Search
Data warehouse model fact table model design
架构设计三原则
[medical] participants to medical ontologies: Content Selection for Clinical Abstract Summarization
一个中年程序员学习中国近代史的小结