Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
taobao-mini-template
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
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
qinhaitao
taobao-mini-template
Commits
952901b2
Commit
952901b2
authored
Jan 11, 2021
by
王波
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加加减乘除去除精度问题方法
parent
ab4044c2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
0 deletions
+80
-0
math.ts
v2.0/src/utils/common/math.ts
+78
-0
index.ts
v2.0/src/utils/index.ts
+2
-0
No files found.
v2.0/src/utils/common/math.ts
0 → 100644
View file @
952901b2
/** @format */
//加法
export
function
add
(
num1
:
number
,
num2
:
number
):
number
{
const
num1Decimal
=
num1
.
toString
().
split
(
'.'
)[
1
]
||
''
const
num2Decimal
=
num2
.
toString
().
split
(
'.'
)[
1
]
||
''
const
num1DecimalLength
=
num1Decimal
.
length
const
num2DecimalLength
=
num2Decimal
.
length
//计算小数位数位数差值
if
(
num1DecimalLength
>=
num2DecimalLength
)
{
//如果前一位比后一位大,则后一位补位计算精度
return
(
(
num1
*
Math
.
pow
(
10
,
num1DecimalLength
+
1
)
+
num2
*
Math
.
pow
(
10
,
num2DecimalLength
+
1
)
*
Math
.
pow
(
10
,
num1DecimalLength
-
num2DecimalLength
))
/
Math
.
pow
(
10
,
num1DecimalLength
+
1
)
)
}
else
{
return
(
(
num2
*
Math
.
pow
(
10
,
num2DecimalLength
+
1
)
+
num1
*
Math
.
pow
(
10
,
num1DecimalLength
+
1
)
*
Math
.
pow
(
10
,
num2DecimalLength
-
num1DecimalLength
))
/
Math
.
pow
(
10
,
num2DecimalLength
+
1
)
)
}
}
//减法
export
function
subtract
(
num1
:
number
,
num2
:
number
):
number
{
const
num1Decimal
=
num1
.
toString
().
split
(
'.'
)[
1
]
||
''
const
num2Decimal
=
num2
.
toString
().
split
(
'.'
)[
1
]
||
''
const
num1DecimalLength
=
num1Decimal
.
length
const
num2DecimalLength
=
num2Decimal
.
length
//计算小数位数位数差值
if
(
num1DecimalLength
>=
num2DecimalLength
)
{
//如果前一位比后一位大,则后一位补位计算精度
return
(
(
num1
*
Math
.
pow
(
10
,
num1DecimalLength
+
1
)
-
num2
*
Math
.
pow
(
10
,
num2DecimalLength
+
1
)
*
Math
.
pow
(
10
,
num1DecimalLength
-
num2DecimalLength
))
/
Math
.
pow
(
10
,
num1DecimalLength
+
1
)
)
}
else
{
return
(
(
num1
*
Math
.
pow
(
10
,
num1DecimalLength
+
1
)
*
Math
.
pow
(
10
,
num2DecimalLength
-
num1DecimalLength
)
-
num2
*
Math
.
pow
(
10
,
num2DecimalLength
+
1
))
/
Math
.
pow
(
10
,
num2DecimalLength
+
1
)
)
}
}
//乘法
export
function
multiply
(
num1
:
number
,
num2
:
number
):
number
{
const
num1Decimal
=
num1
.
toString
().
split
(
'.'
)[
1
]
||
''
const
num2Decimal
=
num2
.
toString
().
split
(
'.'
)[
1
]
||
''
const
num1DecimalLength
=
num1Decimal
.
length
const
num2DecimalLength
=
num2Decimal
.
length
//计算小数位数位数差值
return
(
(
num1
*
Math
.
pow
(
10
,
num1DecimalLength
+
1
)
*
(
num2
*
Math
.
pow
(
10
,
num2DecimalLength
+
1
)))
/
Math
.
pow
(
10
,
num1DecimalLength
+
num2DecimalLength
+
2
)
)
}
//除法
export
function
divide
(
num1
:
number
,
num2
:
number
):
number
{
const
num1Decimal
=
num1
.
toString
().
split
(
'.'
)[
1
]
||
''
const
num2Decimal
=
num2
.
toString
().
split
(
'.'
)[
1
]
||
''
const
num1DecimalLength
=
num1Decimal
.
length
const
num2DecimalLength
=
num2Decimal
.
length
//计算小数位数位数差值
if
(
num1DecimalLength
>=
num2DecimalLength
)
{
//如果前一位比后一位大,则后一位补位计算精度
return
(
(
num1
*
Math
.
pow
(
10
,
num1DecimalLength
+
1
))
/
(
num2
*
Math
.
pow
(
10
,
num2DecimalLength
+
1
)
*
Math
.
pow
(
10
,
num1DecimalLength
-
num2DecimalLength
))
)
}
else
{
return
(
(
num1
*
Math
.
pow
(
10
,
num1DecimalLength
+
1
)
*
Math
.
pow
(
10
,
num2DecimalLength
-
num1DecimalLength
))
/
(
num2
*
Math
.
pow
(
10
,
num2DecimalLength
+
1
))
)
}
}
v2.0/src/utils/index.ts
View file @
952901b2
...
...
@@ -27,3 +27,5 @@ export * from './common/userUpdate'
export
*
from
'./common/format'
export
*
from
'./common/doHandler'
export
*
from
'./common/math'
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