Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
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
YZG
yzg-util
Commits
b2f16228
Commit
b2f16228
authored
Oct 19, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
升级新版本
parent
692c1d8c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
2 deletions
+38
-2
StringHelper.java
...c/main/java/com/yanzuoguang/util/helper/StringHelper.java
+38
-2
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/StringHelper.java
View file @
b2f16228
...
...
@@ -5,9 +5,11 @@ import com.yanzuoguang.util.base.ObjectHelper;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
java.io.UnsupportedEncodingException
;
import
java.math.BigDecimal
;
import
java.nio.charset.StandardCharsets
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
import
java.text.DecimalFormat
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
...
...
@@ -444,7 +446,16 @@ public class StringHelper {
}
}
private
static
boolean
isBaseType
(
String
clsName
,
Class
<?>
cls
,
Class
<?>
fromCls
,
Object
fromValue
)
{
/**
* 是否属于基础类型
*
* @param clsName 基础类型名称
* @param cls 基础类型
* @param fromCls 来源类型
* @param fromValue 基础类型值
* @return 是否属于基础类型
*/
public
static
boolean
isBaseType
(
String
clsName
,
Class
<?>
cls
,
Class
<?>
fromCls
,
Object
fromValue
)
{
if
(
clsName
.
equals
(
fromCls
.
getName
()))
{
return
true
;
}
...
...
@@ -461,7 +472,32 @@ public class StringHelper {
* @return 转换成功的字符串
*/
public
static
String
toString
(
Object
obj
)
{
return
obj
==
null
?
null
:
obj
.
toString
();
Class
<?>
fromCls
=
obj
!=
null
?
obj
.
getClass
()
:
Object
.
class
;
boolean
isDouble
=
StringHelper
.
isBaseType
(
StringHelper
.
TYPE_DOUBLE
,
Double
.
class
,
fromCls
,
obj
);
boolean
isFromDouble
=
obj
instanceof
BigDecimal
||
isDouble
;
if
(
isFromDouble
)
{
// 格式化设置(显示所有整数部分,小数点保留2位)
DecimalFormat
decimalFormat
=
new
DecimalFormat
(
"#.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
);
String
to
=
decimalFormat
.
format
(
obj
);
String
[]
split
=
to
.
split
(
"\\."
);
StringBuilder
sb
=
new
StringBuilder
(
split
[
0
]);
if
(
sb
.
length
()
==
0
)
{
sb
.
append
(
"0"
);
}
if
(
split
.
length
>
1
)
{
// 去掉右边的0
String
right
=
StringHelper
.
trimRight
(
split
[
1
],
"0"
);
if
(!
right
.
isEmpty
())
{
sb
.
append
(
"."
);
sb
.
append
(
right
);
}
}
return
sb
.
toString
();
}
else
{
return
obj
==
null
?
null
:
obj
.
toString
();
}
}
/**
...
...
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