Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
finance-manage
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
finance-oa
finance-manage
Commits
f67d6823
Commit
f67d6823
authored
May 06, 2021
by
若依
Browse files
Options
Browse Files
Download
Plain Diff
!221 update ruoyi-common/src/main/java/com/ruoyi/common/utils/Arith.java.
Merge pull request !221 from phper08/N/A
parents
62081aeb
7d0f5e94
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
114 additions
and
114 deletions
+114
-114
ruoyi-common/src/main/java/com/ruoyi/common/utils/Arith.java
ruoyi-common/src/main/java/com/ruoyi/common/utils/Arith.java
+114
-114
No files found.
ruoyi-common/src/main/java/com/ruoyi/common/utils/Arith.java
View file @
f67d6823
package
com.ruoyi.common.utils
;
package
com.ruoyi.common.utils
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
java.math.RoundingMode
;
/**
/**
* 精确的浮点数运算
* 精确的浮点数运算
*
*
* @author ruoyi
* @author ruoyi
*/
*/
public
class
Arith
public
class
Arith
{
{
/** 默认除法运算精度 */
/** 默认除法运算精度 */
private
static
final
int
DEF_DIV_SCALE
=
10
;
private
static
final
int
DEF_DIV_SCALE
=
10
;
/** 这个类不能实例化 */
/** 这个类不能实例化 */
private
Arith
()
private
Arith
()
{
{
}
}
/**
/**
* 提供精确的加法运算。
* 提供精确的加法运算。
* @param v1 被加数
* @param v1 被加数
* @param v2 加数
* @param v2 加数
* @return 两个参数的和
* @return 两个参数的和
*/
*/
public
static
double
add
(
double
v1
,
double
v2
)
public
static
double
add
(
double
v1
,
double
v2
)
{
{
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
return
b1
.
add
(
b2
).
doubleValue
();
return
b1
.
add
(
b2
).
doubleValue
();
}
}
/**
/**
* 提供精确的减法运算。
* 提供精确的减法运算。
* @param v1 被减数
* @param v1 被减数
* @param v2 减数
* @param v2 减数
* @return 两个参数的差
* @return 两个参数的差
*/
*/
public
static
double
sub
(
double
v1
,
double
v2
)
public
static
double
sub
(
double
v1
,
double
v2
)
{
{
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
return
b1
.
subtract
(
b2
).
doubleValue
();
return
b1
.
subtract
(
b2
).
doubleValue
();
}
}
/**
/**
* 提供精确的乘法运算。
* 提供精确的乘法运算。
* @param v1 被乘数
* @param v1 被乘数
* @param v2 乘数
* @param v2 乘数
* @return 两个参数的积
* @return 两个参数的积
*/
*/
public
static
double
mul
(
double
v1
,
double
v2
)
public
static
double
mul
(
double
v1
,
double
v2
)
{
{
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
return
b1
.
multiply
(
b2
).
doubleValue
();
return
b1
.
multiply
(
b2
).
doubleValue
();
}
}
/**
/**
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
* 小数点以后10位,以后的数字四舍五入。
* 小数点以后10位,以后的数字四舍五入。
* @param v1 被除数
* @param v1 被除数
* @param v2 除数
* @param v2 除数
* @return 两个参数的商
* @return 两个参数的商
*/
*/
public
static
double
div
(
double
v1
,
double
v2
)
public
static
double
div
(
double
v1
,
double
v2
)
{
{
return
div
(
v1
,
v2
,
DEF_DIV_SCALE
);
return
div
(
v1
,
v2
,
DEF_DIV_SCALE
);
}
}
/**
/**
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
* 定精度,以后的数字四舍五入。
* 定精度,以后的数字四舍五入。
* @param v1 被除数
* @param v1 被除数
* @param v2 除数
* @param v2 除数
* @param scale 表示表示需要精确到小数点以后几位。
* @param scale 表示表示需要精确到小数点以后几位。
* @return 两个参数的商
* @return 两个参数的商
*/
*/
public
static
double
div
(
double
v1
,
double
v2
,
int
scale
)
public
static
double
div
(
double
v1
,
double
v2
,
int
scale
)
{
{
if
(
scale
<
0
)
if
(
scale
<
0
)
{
{
throw
new
IllegalArgumentException
(
throw
new
IllegalArgumentException
(
"The scale must be a positive integer or zero"
);
"The scale must be a positive integer or zero"
);
}
}
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b1
=
new
BigDecimal
(
Double
.
toString
(
v1
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
BigDecimal
b2
=
new
BigDecimal
(
Double
.
toString
(
v2
));
if
(
b1
.
compareTo
(
BigDecimal
.
ZERO
)
==
0
)
if
(
b1
.
compareTo
(
BigDecimal
.
ZERO
)
==
0
)
{
{
return
BigDecimal
.
ZERO
.
doubleValue
();
return
BigDecimal
.
ZERO
.
doubleValue
();
}
}
return
b1
.
divide
(
b2
,
scale
,
RoundingMode
.
HALF_UP
).
doubleValue
();
return
b1
.
divide
(
b2
,
scale
,
RoundingMode
.
HALF_UP
).
doubleValue
();
}
}
/**
/**
* 提供精确的小数位四舍五入处理。
* 提供精确的小数位四舍五入处理。
* @param v 需要四舍五入的数字
* @param v 需要四舍五入的数字
* @param scale 小数点后保留几位
* @param scale 小数点后保留几位
* @return 四舍五入后的结果
* @return 四舍五入后的结果
*/
*/
public
static
double
round
(
double
v
,
int
scale
)
public
static
double
round
(
double
v
,
int
scale
)
{
{
if
(
scale
<
0
)
if
(
scale
<
0
)
{
{
throw
new
IllegalArgumentException
(
throw
new
IllegalArgumentException
(
"The scale must be a positive integer or zero"
);
"The scale must be a positive integer or zero"
);
}
}
BigDecimal
b
=
new
BigDecimal
(
Double
.
toString
(
v
));
BigDecimal
b
=
new
BigDecimal
(
Double
.
toString
(
v
));
BigDecimal
one
=
new
BigDecimal
(
"1"
);
BigDecimal
one
=
BigDecimal
.
ONE
;
return
b
.
divide
(
one
,
scale
,
RoundingMode
.
HALF_UP
).
doubleValue
();
return
b
.
divide
(
one
,
scale
,
RoundingMode
.
HALF_UP
).
doubleValue
();
}
}
}
}
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