Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
F
fe-utils
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
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
sunguoshu
fe-utils
Commits
10ed5d6b
Commit
10ed5d6b
authored
Jul 18, 2023
by
niuyutian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
代码提交
parent
bb7b1969
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
79 additions
and
1 deletion
+79
-1
numberToWords3.ts
src/money/numberToWords3.ts
+76
-0
numberToWords.test.ts
test/money/numberToWords.test.ts
+3
-1
No files found.
src/money/numberToWords3.ts
0 → 100644
View file @
10ed5d6b
export
default
function
numberToWords
(
input
:
string
|
number
):
string
|
boolean
{
//首先由于number去判断会有精度问题 如0.1+0.2!=0.3 这种会精度缺失,这里全使用字符串判断
let
num
=
String
(
input
)
if
(
num
==
''
||
num
==
'0'
||
num
==
'0.0'
||
num
==
'0.00'
){
return
'零元整'
}
let
dotArr
=
[
'角'
,
'分'
]
//, '厘', '毫', '丝'
let
digitArr
=
[
'零'
,
'壹'
,
'贰'
,
'叁'
,
'肆'
,
'伍'
,
'陆'
,
'柒'
,
'捌'
,
'玖'
]
let
unitArr
=
[
[
''
,
'拾'
,
'佰'
,
'仟'
],
[
''
,
'万'
,
'亿'
,
'兆'
]
]
let
intStr
=
''
,
dotStr
=
''
//记录整数字符串 记录小数字符串
//获取头部,是否为负数
let
head
=
num
[
0
]
==
'-'
?
'负'
:
''
//根据.将整数小数进行拆分
const
arr
=
num
.
split
(
'.'
)
let
int
=
arr
[
0
]
||
''
,
dot
=
arr
[
1
]
||
''
//分成整数和小数两个部分
//如果dot数组长度大于零并且有值的情况下,那么我就把他拼接成有单位的字符串
if
(
dot
.
length
>
0
)
{
for
(
let
i
=
0
;
i
<
dotArr
.
length
&&
dot
[
i
];
i
++
)
{
dotStr
+=
digitArr
[
dot
[
i
]]
+
dotArr
[
i
]
}
}
//翻转输入的金额,然后去判断(因为从大到小很难做,这里从小往大去算)
int
=
int
.
split
(
''
).
reverse
().
join
(
''
)
// 因为这里的展示情况十个百千 是重复的 只是最后的单位不重复 所以这里使用四位去分割然后读取
// 个十百千 万十百千 亿十百千
for
(
let
i
=
0
;
i
<
int
.
length
;
i
++
)
{
let
unitIndex
=
Math
.
floor
(
i
/
4
)
//获取 空万亿的索引
let
decadeIndex
=
i
%
4
//获取个十百千的索引
let
cash
=
digitArr
[
int
[
i
]]
let
unit
=
''
;
if
(
cash
!==
'零'
)
{
unit
=
unitArr
[
0
][
decadeIndex
]
if
(
intStr
.
indexOf
(
unitArr
[
1
][
unitIndex
])
==
-
1
)
{
//这里是因为后面我需要将连续两个以上的零替换为一个所以这里如果是零后面就不跟单位了
//但是可能又存在十亿元需要单位所以这里只要是我有值并且没有当前unitIndex索引的单位那么就加上,
unit
+=
unitArr
[
1
][
unitIndex
]
}
}
//拼接值+单位
intStr
=
cash
+
unit
+
intStr
}
//将连续出现的多个零换成零,
intStr
=
intStr
.
replace
(
/零+/g
,
'零'
)
intStr
+=
'元'
//如果是零元 直接改成元
intStr
=
intStr
.
replace
(
/零元/
,
'元'
)
//如果只有元置空
if
(
intStr
===
'元'
)
{
intStr
=
''
}
/**
* 这里有几种情况
* 1. 当角 分 都为0 或者dotStr是空的时候 直接返回'整'字符串
* 2. 当角为0分为正常且整数为零 则是x分
* 3. 当角为0分为正常且整数不为零 则是xx元零x分
* 4. 当角正常 分为零 则 x角
*/
if
((
dotStr
[
0
]
==
'零'
&&
dotStr
[
2
]
==
'零'
)
||
dotStr
==
''
)
{
dotStr
=
'整'
}
else
if
(
dotStr
[
0
]
==
'零'
&&
intStr
!==
''
)
{
dotStr
=
dotStr
.
replace
(
'角'
,
''
)
}
else
if
(
dotStr
[
0
]
==
'零'
&&
intStr
==
''
)
{
dotStr
=
dotStr
.
replace
(
'零角'
,
''
)
}
else
if
(
dotStr
[
2
]
==
'零'
)
{
dotStr
=
dotStr
.
slice
(
0
,
2
)
}
//拼接 正负 + 整数 + 小数
return
head
+
intStr
+
dotStr
}
\ No newline at end of file
test/money/numberToWords.test.ts
View file @
10ed5d6b
import
{
describe
,
it
,
expect
}
from
'vitest'
import
numberToWords
from
'../../src/money/numberToWords'
import
numberToWords2
from
'../../src/money/numberToWords2'
import
numberToWords3
from
'../../src/money/numberToWords3'
describe
(
'测试整数'
,
()
=>
{
it
(
'0 toBe 零元整'
,
()
=>
{
...
...
@@ -264,7 +266,7 @@ describe('随机数测试', () => {
it
(
'随机数测试'
,
()
=>
{
for
(
let
i
=
0
;
i
<
10000
;
i
++
)
{
const
random
=
Math
.
random
()
*
1000000000000
const
result
=
numberToWords
(
random
)
const
result
=
numberToWords
3
(
random
)
const
result2
=
numberToWords2
(
random
)
console
.
log
(
random
,
result
,
result2
)
expect
(
result
).
toBe
(
result2
)
...
...
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