Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-micro
Commits
e9eeaf03
Commit
e9eeaf03
authored
Jan 15, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加functions文件
parent
86ed5dc1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
172 additions
and
1 deletion
+172
-1
composer.json
composer.json
+4
-1
functions.php
src/functions.php
+168
-0
No files found.
composer.json
View file @
e9eeaf03
...
@@ -16,6 +16,9 @@
...
@@ -16,6 +16,9 @@
"autoload"
:
{
"autoload"
:
{
"psr-4"
:
{
"psr-4"
:
{
"Meibuyu\\Micro\\"
:
"src/"
"Meibuyu\\Micro\\"
:
"src/"
}
},
"files"
:
[
"src/functions.php"
]
}
}
}
}
src/functions.php
0 → 100644
View file @
e9eeaf03
<?php
if
(
!
function_exists
(
'decimal_to_abc'
))
{
/**
* 数字转换对应26个字母
* @param $num
* @return string
*/
function
decimal_to_abc
(
$num
)
{
$str
=
""
;
$ten
=
$num
;
if
(
$ten
==
0
)
return
"A"
;
while
(
$ten
!=
0
)
{
$x
=
$ten
%
26
;
$str
.=
chr
(
65
+
$x
);
$ten
=
intval
(
$ten
/
26
);
}
return
strrev
(
$str
);
}
}
if
(
!
function_exists
(
'diff_between_two_days'
))
{
/**
* 计算两个日期之间相差的天数
* @param $day1
* @param $day2
* @return float|int
*/
function
diff_between_two_days
(
$day1
,
$day2
)
{
$second1
=
strtotime
(
$day1
);
$second2
=
strtotime
(
$day2
);
return
round
((
abs
(
$second1
-
$second2
)
/
86400
),
0
);
}
}
if
(
!
function_exists
(
'decimals_to_percentage'
))
{
/**
* 将小数转换百分数
* @param float $decimals 小数
* @param int $num 保留小数位
* @return string
*/
function
decimals_to_percentage
(
$decimals
,
$num
=
2
)
{
return
sprintf
(
"%01."
.
$num
.
"f"
,
$decimals
*
100
)
.
'%'
;
}
}
if
(
!
function_exists
(
'calculate_grade'
))
{
/**
*
* 计算一个数的区间范围等级
* @param array $range 区间范围(从大到小排列)
* @param $num
* @return mixed|void
*/
function
calculate_grade
(
$range
,
$num
)
{
$max
=
max
(
$range
);
if
(
$num
>=
$max
)
{
return
count
(
$range
);
}
foreach
(
$range
as
$k
=>
$v
)
{
if
(
$num
<
$v
)
{
return
$k
;
}
}
}
}
if
(
!
function_exists
(
'convertAmountToCn'
))
{
/**
* 2 * 将数值金额转换为中文大写金额
* 3 * @param $amount float 金额(支持到分)
* 4 * @param $type int 补整类型,0:到角补整;1:到元补整
* 5 * @return mixed 中文大写金额
* 6 */
function
convertAmountToCn
(
$amount
,
$type
=
1
)
{
// 判断输出的金额是否为数字或数字字符串
if
(
!
is_numeric
(
$amount
))
{
return
"要转换的金额只能为数字!"
;
}
// 金额为0,则直接输出"零元整"
if
(
$amount
==
0
)
{
return
"人民币零元整"
;
}
// 金额不能为负数
if
(
$amount
<
0
)
{
return
"要转换的金额不能为负数!"
;
}
// 金额不能超过万亿,即12位
if
(
strlen
(
$amount
)
>
12
)
{
return
"要转换的金额不能为万亿及更高金额!"
;
}
// 预定义中文转换的数组
$digital
=
array
(
'零'
,
'壹'
,
'贰'
,
'叁'
,
'肆'
,
'伍'
,
'陆'
,
'柒'
,
'捌'
,
'玖'
);
// 预定义单位转换的数组
$position
=
array
(
'仟'
,
'佰'
,
'拾'
,
'亿'
,
'仟'
,
'佰'
,
'拾'
,
'万'
,
'仟'
,
'佰'
,
'拾'
,
'元'
);
// 将金额的数值字符串拆分成数组
$amountArr
=
explode
(
'.'
,
$amount
);
// 将整数位的数值字符串拆分成数组
$integerArr
=
str_split
(
$amountArr
[
0
],
1
);
// 将整数部分替换成大写汉字
$result
=
'人民币'
;
$integerArrLength
=
count
(
$integerArr
);
// 整数位数组的长度
$positionLength
=
count
(
$position
);
// 单位数组的长度
for
(
$i
=
0
;
$i
<
$integerArrLength
;
$i
++
)
{
// 如果数值不为0,则正常转换
if
(
$integerArr
[
$i
]
!=
0
)
{
$result
=
$result
.
$digital
[
$integerArr
[
$i
]]
.
$position
[
$positionLength
-
$integerArrLength
+
$i
];
}
else
{
// 如果数值为0, 且单位是亿,万,元这三个的时候,则直接显示单位
if
((
$positionLength
-
$integerArrLength
+
$i
+
1
)
%
4
==
0
)
{
$result
=
$result
.
$position
[
$positionLength
-
$integerArrLength
+
$i
];
}
}
}
// 如果小数位也要转换
if
(
$type
==
0
)
{
// 将小数位的数值字符串拆分成数组
$decimalArr
=
str_split
(
$amountArr
[
1
],
1
);
// 将角替换成大写汉字. 如果为0,则不替换
if
(
$decimalArr
[
0
]
!=
0
)
{
$result
=
$result
.
$digital
[
$decimalArr
[
0
]]
.
'角'
;
}
// 将分替换成大写汉字. 如果为0,则不替换
if
(
$decimalArr
[
1
]
!=
0
)
{
$result
=
$result
.
$digital
[
$decimalArr
[
1
]]
.
'分'
;
}
}
else
{
$result
=
$result
.
'整'
;
}
return
$result
;
}
}
if
(
!
function_exists
(
'today'
))
{
/**
* Create a new Carbon instance for the current time.
* @return false|string
*/
function
today
()
{
return
date
(
'Y-m-d'
,
time
());
}
}
if
(
!
function_exists
(
'now'
))
{
/**
* Create a new Carbon instance for the current time.
* @return false|string
*/
function
now
()
{
return
date
(
'Y-m-d h:i:s'
,
time
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment