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
12e8c6a1
Commit
12e8c6a1
authored
Oct 19, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
升级新版本
parent
6c26afda
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
2 deletions
+60
-2
StringHelper.java
...c/main/java/com/yanzuoguang/util/helper/StringHelper.java
+38
-2
TestStringHelper.java
yzg-util-base/src/test/java/helper/TestStringHelper.java
+22
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/StringHelper.java
View file @
12e8c6a1
...
@@ -5,9 +5,11 @@ import com.yanzuoguang.util.base.ObjectHelper;
...
@@ -5,9 +5,11 @@ import com.yanzuoguang.util.base.ObjectHelper;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
java.io.UnsupportedEncodingException
;
import
java.io.UnsupportedEncodingException
;
import
java.math.BigDecimal
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.charset.StandardCharsets
;
import
java.security.MessageDigest
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.NoSuchAlgorithmException
;
import
java.text.DecimalFormat
;
import
java.util.*
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
...
@@ -444,7 +446,16 @@ public class StringHelper {
...
@@ -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
()))
{
if
(
clsName
.
equals
(
fromCls
.
getName
()))
{
return
true
;
return
true
;
}
}
...
@@ -461,7 +472,32 @@ public class StringHelper {
...
@@ -461,7 +472,32 @@ public class StringHelper {
* @return 转换成功的字符串
* @return 转换成功的字符串
*/
*/
public
static
String
toString
(
Object
obj
)
{
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
();
}
}
}
/**
/**
...
...
yzg-util-base/src/test/java/helper/TestStringHelper.java
View file @
12e8c6a1
...
@@ -4,6 +4,8 @@ import com.yanzuoguang.util.helper.StringHelper;
...
@@ -4,6 +4,8 @@ import com.yanzuoguang.util.helper.StringHelper;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.Test
;
import
java.math.BigDecimal
;
public
class
TestStringHelper
{
public
class
TestStringHelper
{
@Test
@Test
public
void
testFirstRun
()
{
public
void
testFirstRun
()
{
...
@@ -38,5 +40,25 @@ public class TestStringHelper {
...
@@ -38,5 +40,25 @@ public class TestStringHelper {
System
.
out
.
println
(
StringHelper
.
trim
(
"1005050103"
,
"1"
,
"0"
,
"3"
));
System
.
out
.
println
(
StringHelper
.
trim
(
"1005050103"
,
"1"
,
"0"
,
"3"
));
}
}
@Test
public
void
testToString
()
{
System
.
out
.
println
(
StringHelper
.
toString
(
0.0000000
));
System
.
out
.
println
(
StringHelper
.
toString
(
50.0000001
));
System
.
out
.
println
(
StringHelper
.
toString
(
0.02
));
System
.
out
.
println
(
StringHelper
.
toString
(
0.0000001
));
System
.
out
.
println
(
StringHelper
.
toString
(
50.00050000
));
System
.
out
.
println
(
StringHelper
.
toString
(
0.00050000
));
System
.
out
.
println
(
StringHelper
.
toString
(
0.1000000
));
System
.
out
.
println
(
StringHelper
.
toString
(
5.1000000
));
System
.
out
.
println
(
StringHelper
.
toString
(
100.1000000
));
System
.
out
.
println
(
StringHelper
.
toString
(
new
BigDecimal
(
"0.0000000"
)));
System
.
out
.
println
(
StringHelper
.
toString
(
new
BigDecimal
(
"0.0000001"
)));
System
.
out
.
println
(
StringHelper
.
toString
(
new
BigDecimal
(
"0.00050000"
)));
System
.
out
.
println
(
StringHelper
.
toString
(
new
BigDecimal
(
"0.1000000"
)));
System
.
out
.
println
(
StringHelper
.
toString
(
new
BigDecimal
(
"100.1000000"
)));
}
}
}
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