Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
ptfw-uniapp
Project
Project
Details
Activity
Releases
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
侯忠英
ptfw-uniapp
Commits
cdfe16b4
Commit
cdfe16b4
authored
Aug 05, 2019
by
刘攀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复精度计算问题
parent
da349f08
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
6 deletions
+66
-6
util.js
src/utils/basic/util.js
+66
-6
No files found.
src/utils/basic/util.js
View file @
cdfe16b4
...
...
@@ -174,16 +174,76 @@ class Util {
// 数字运算,处理精度问题
numCalc
(
type
,
num1
,
num2
)
{
// 最多保留两位小数,不四舍五入
num1
=
Math
.
floor
(
Number
(
num1
)
*
100
)
num2
=
Math
.
floor
(
Number
(
num2
)
*
100
)
if
(
type
===
'+'
)
{
return
(
num1
+
num2
)
/
100
return
add
(
num1
,
num2
)
}
else
if
(
type
===
'-'
)
{
return
(
num1
-
num2
)
/
100
return
sub
(
num1
,
num2
)
}
else
if
(
type
===
'*'
)
{
return
(
num1
*
num2
)
/
100
return
mul
(
num1
,
num2
)
}
else
if
(
type
===
'/'
)
{
return
METHOD
.
notRoundedToFixed
(
num1
/
num2
/
100
)
return
div
(
num1
,
num2
)
}
function
add
(
org1
,
org2
)
{
var
r1
,
r2
,
m
try
{
r1
=
Number
(
org1
)
.
toString
()
.
split
(
'.'
)[
1
].
length
}
catch
(
e
)
{
r1
=
0
}
try
{
r2
=
Number
(
org2
)
.
toString
()
.
split
(
'.'
)[
1
].
length
}
catch
(
e
)
{
r2
=
0
}
m
=
Math
.
pow
(
10
,
Math
.
max
(
r1
,
r2
))
return
(
Number
(
org1
)
*
m
+
Number
(
org2
)
*
m
)
/
m
}
function
sub
(
r1
,
r2
)
{
return
add
(
r1
,
-
r2
)
}
function
mul
(
org1
,
org2
)
{
var
m
=
0
,
s1
=
Number
(
org1
).
toString
(),
s2
=
Number
(
org2
).
toString
()
try
{
m
+=
s1
.
split
(
'.'
)[
1
].
length
}
catch
(
e
)
{}
try
{
m
+=
s2
.
split
(
'.'
)[
1
].
length
}
catch
(
e
)
{}
return
(
Number
(
s1
.
replace
(
'.'
,
''
))
*
Number
(
s2
.
replace
(
'.'
,
''
)))
/
Math
.
pow
(
10
,
m
)
}
function
div
(
org1
,
org2
)
{
var
t1
=
0
,
t2
=
0
,
r1
,
r2
try
{
t1
=
Number
(
org1
)
.
toString
()
.
split
(
'.'
)[
1
].
length
}
catch
(
e
)
{}
try
{
t2
=
Number
(
org2
)
.
toString
()
.
split
(
'.'
)[
1
].
length
}
catch
(
e
)
{}
r1
=
Number
(
Number
(
org1
)
.
toString
()
.
replace
(
'.'
,
''
)
)
r2
=
Number
(
Number
(
org2
)
.
toString
()
.
replace
(
'.'
,
''
)
)
return
(
r1
/
r2
)
*
Math
.
pow
(
10
,
t2
-
t1
)
}
}
// 金额保留两位小数,不四舍五入
...
...
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