当前位置:网站首页>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 uses ords+pljson to implement JSON_ Table effect
- Oracle段顾问、怎么处理行链接行迁移、降低高水位
- Drawing mechanism of view (II)
- Drawing mechanism of view (3)
- Get the uppercase initials of Chinese Pinyin in PHP
- Typeerror in allenlp: object of type tensor is not JSON serializable error
- 【调参Tricks】WhiteningBERT: An Easy Unsupervised Sentence Embedding Approach
- Explanation of suffix of Oracle EBS standard table
- Proteus -- RS-232 dual computer communication
- Determine whether the version number is continuous in PHP
猜你喜欢

TCP attack

一份Slide两张表格带你快速了解目标检测

Cognitive science popularization of middle-aged people

Oracle apex Ajax process + dy verification

Yaml file of ingress controller 0.47.0

Sparksql data skew

sparksql数据倾斜那些事儿

Ding Dong, here comes the redis om object mapping framework

【信息检索导论】第二章 词项词典与倒排记录表

JSP intelligent community property management system
随机推荐
使用Matlab实现:弦截法、二分法、CG法,求零点、解方程
Explain in detail the process of realizing Chinese text classification by CNN
[model distillation] tinybert: distilling Bert for natural language understanding
华为机试题
Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
架构设计三原则
Oracle general ledger balance table GL for foreign currency bookkeeping_ Balance change (Part 1)
【信息检索导论】第六章 词项权重及向量空间模型
使用 Compose 实现可见 ScrollBar
Using MATLAB to realize: power method, inverse power method (origin displacement)
view的绘制机制(二)
view的绘制机制(三)
Principle analysis of spark
解决万恶的open failed: ENOENT (No such file or directory)/(Operation not permitted)
[torch] the most concise logging User Guide
Open failed: enoent (no such file or directory) / (operation not permitted)
ssm人事管理系统
ERNIE1.0 与 ERNIE2.0 论文解读
华为机试题-20190417
One field in thinkphp5 corresponds to multiple fuzzy queries