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
1d7da811
Commit
1d7da811
authored
Jul 19, 2023
by
sunguoshu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix
parent
f008d11d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
22 deletions
+18
-22
numberToWords2.ts
src/money/numberToWords2.ts
+9
-7
numberToWords.test.ts
test/money/numberToWords.test.ts
+9
-15
No files found.
src/money/numberToWords2.ts
View file @
1d7da811
const
digits
=
[
'零'
,
'壹'
,
'贰'
,
'叁'
,
'肆'
,
'伍'
,
'陆'
,
'柒'
,
'捌'
,
'玖'
]
as
const
const
units
=
[
''
,
'拾'
,
'佰'
,
'仟'
]
as
const
const
units2
=
[
''
,
'万'
,
'亿'
]
as
const
const
units3
=
[
'角'
,
'分'
,
'厘'
]
as
const
const
units3
=
[
'角'
,
'分'
]
as
const
export
default
function
numberToWords
(
input
:
string
|
number
):
string
{
const
number
=
Number
(
input
)
if
(
number
>=
10
**
13
)
throw
new
Error
(
'数字太大'
)
let
[
integer
=
''
,
decimal
=
''
]
=
String
(
input
).
split
(
'.'
)
decimal
=
decimal
.
slice
(
0
,
2
)
const
number
=
Number
(
integer
+
'.'
+
decimal
)
if
(
number
>=
10
**
12
)
throw
new
Error
(
'数字太大'
)
if
(
number
<
0
)
throw
new
Error
(
'数字不能为负数'
)
if
(
number
===
0
)
return
'零元整'
let
[
integer
=
''
,
decimal
=
''
]
=
String
(
number
).
split
(
'.'
)
integer
=
integer
.
padStart
(
12
,
'0'
).
slice
(
-
12
)
decimal
=
decimal
.
padEnd
(
2
,
'0'
).
slice
(
0
,
2
)
;
[
integer
=
''
,
decimal
=
''
]
=
String
(
number
).
split
(
'.'
)
integer
=
integer
.
slice
(
-
12
)
decimal
=
decimal
.
slice
(
0
,
2
)
let
result
=
''
if
(
integer
)
{
let
preNull
=
true
...
...
@@ -39,7 +41,7 @@ export default function numberToWords(input: string | number): string {
}
}
if
(
result
)
result
+=
'元'
if
(
!
decimal
||
decimal
===
'00'
)
return
result
+
'整'
if
(
!
decimal
)
return
result
+
'整'
if
(
decimal
)
{
for
(
let
i
=
0
;
i
<
decimal
.
length
;
i
++
)
{
const
digit
=
digits
[
Number
(
decimal
[
i
])]
...
...
test/money/numberToWords.test.ts
View file @
1d7da811
import
{
describe
,
it
,
expect
}
from
'vitest'
import
numberToWords
from
'../../src/money/numberToWords'
import
numberToWords
2
from
'../../src/money/numberToWords2'
import
numberToWords
2
from
'../../src/money/numberToWords'
import
numberToWords
from
'../../src/money/numberToWords2'
import
numberToWords3
from
'../../src/money/numberToWords3'
describe
(
'测试整数'
,
()
=>
{
...
...
@@ -229,6 +229,9 @@ describe('测试小数', () => {
})
describe
(
'测试极端小数'
,
()
=>
{
it
(
'0.001 toBe 零元整'
,
()
=>
{
expect
(
numberToWords
(
0.001
)).
toBe
(
'零元整'
)
})
it
(
'1.001 toBe 壹元整'
,
()
=>
{
expect
(
numberToWords
(
1.001
)).
toBe
(
'壹元整'
)
})
...
...
@@ -259,23 +262,14 @@ describe('小数整数', () => {
it
(
'839109939.677061 toBe 捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分'
,
()
=>
{
expect
(
numberToWords
(
839109939.677061
)).
toBe
(
'捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分'
)
})
it
(
'839109939.677061 toBe 捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分'
,
()
=>
{
expect
(
numberToWords2
(
839109939.677061
)).
toBe
(
'捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分'
)
})
it
(
'839109939.677061 toBe 捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分'
,
()
=>
{
expect
(
numberToWords3
(
839109939.677061
)).
toBe
(
'捌亿叁仟玖佰壹拾万零玖仟玖佰叁拾玖元陆角柒分'
)
})
})
describe
(
'测试特殊情况'
,
()
=>
{
it
(
'-1 toBe error'
,
()
=>
{
expect
(
numberToWords
(
-
1
)).
toBe
(
'负壹元整'
)
})
it
(
'-1 toBe error'
,
()
=>
{
expect
(()
=>
numberToWords2
(
-
1
)).
toThrowError
(
'数字不能为负数'
)
expect
(()
=>
numberToWords
(
-
1
)).
toThrowError
()
})
it
(
'
-1
toBe error'
,
()
=>
{
expect
(
numberToWords3
(
-
1
)).
toBe
(
'负壹元整'
)
it
(
'
1000000000000
toBe error'
,
()
=>
{
expect
(
()
=>
numberToWords
(
1000000000000
)).
toThrowError
(
)
})
})
...
...
@@ -284,7 +278,7 @@ describe('随机数测试', () => {
for
(
let
i
=
0
;
i
<
10000
;
i
++
)
{
const
random
=
Math
.
random
()
*
1000000000000
const
result
=
numberToWords3
(
random
)
const
result2
=
numberToWords
2
(
random
)
const
result2
=
numberToWords
(
random
)
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