Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
MiYaFastCashier
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jiangjiantao
MiYaFastCashier
Commits
81c5d3fb
Commit
81c5d3fb
authored
Feb 28, 2023
by
赵鹏翔
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
d928554c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
150 additions
and
166 deletions
+150
-166
StringPriceFormatUtils.java
...m/fastcashier/lib_common/util/StringPriceFormatUtils.java
+0
-166
StringPriceFormatUtils.kt
...com/fastcashier/lib_common/util/StringPriceFormatUtils.kt
+150
-0
No files found.
lib_common/src/main/java/com/fastcashier/lib_common/util/StringPriceFormatUtils.java
deleted
100644 → 0
View file @
d928554c
package
com
.
fastcashier
.
lib_common
.
util
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
java.text.DecimalFormat
;
public
class
StringPriceFormatUtils
{
/**
* 分转元
*
* @param p
* @return
*/
public
static
String
transStringPriceToDecimalString
(
String
p
)
{
DecimalFormat
numberFormat
=
new
DecimalFormat
(
"0.00"
);
//防止参数不合法异常崩溃
try
{
BigDecimal
price
=
new
BigDecimal
(
p
);
BigDecimal
x
=
new
BigDecimal
(
"100"
);
return
price
.
divide
(
x
,
2
,
RoundingMode
.
HALF_UP
).
toString
();
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
//空或转换异常则显示--.--
return
"--.--"
;
}
}
public
static
String
transDoubleStringPriceToDecimalString
(
String
p
)
{
DecimalFormat
numberFormat
=
new
DecimalFormat
(
"0.00"
);
//防止参数不合法异常崩溃
try
{
Double
price
=
Double
.
parseDouble
(
p
);
return
numberFormat
.
format
(
price
/
100.00
);
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
//空或转换异常则显示--.--
return
"--.--"
;
}
}
public
static
String
transStringPriceToDecimalString
(
double
p
)
{
DecimalFormat
numberFormat
=
new
DecimalFormat
(
"0.00"
);
//防止参数不合法异常崩溃
try
{
return
numberFormat
.
format
(
p
/
100.00
);
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
//空或转换异常则显示--.--
return
"--.--"
;
}
}
/**
* 字符元转字符分
*
* @param yuan 字符元
* @return 字符分
*/
public
static
String
transStringYuan2Fen
(
String
yuan
)
{
String
result
=
yuan
;
String
tmp1
=
""
,
tmp2
=
""
;
if
(
yuan
.
contains
(
"."
))
{
int
index
=
yuan
.
indexOf
(
"."
);
tmp1
=
yuan
.
substring
(
0
,
index
);
tmp2
=
yuan
.
substring
(
index
+
1
,
yuan
.
length
());
result
=
yuan
.
replace
(
"."
,
""
);
if
(
tmp2
.
length
()
<=
0
)
{
result
=
result
+
"00"
;
}
else
if
(
tmp2
.
length
()
==
1
)
{
result
=
result
+
"0"
;
}
else
if
(
tmp2
.
length
()
>
2
)
{
//如果小数点后有3位或以上说明服务器出现异常,此时仍然只取前两位
result
=
result
.
substring
(
0
,
index
+
2
);
}
}
else
{
result
=
result
+
"00"
;
}
return
result
;
}
/**
* 获取实付金额,单位:分
*
* @param totalPrice 总价,单位:元
* @param promPrice 优惠价,单位:元
* @return 实付金额,单位:分
*/
public
static
String
getPayPriceFenStringFromYuan
(
String
totalPrice
,
String
promPrice
)
{
try
{
int
totalPriceInt
=
Integer
.
parseInt
(
transStringYuan2Fen
(
totalPrice
));
int
promPriceInt
=
Integer
.
parseInt
(
transStringYuan2Fen
(
promPrice
));
int
payPriceInt
=
totalPriceInt
-
promPriceInt
;
//检查数字正确性
if
(
totalPriceInt
<
promPriceInt
||
totalPriceInt
<
0
||
promPriceInt
<
0
||
payPriceInt
<
0
)
{
//参数不正确,返回0
return
"0"
;
}
return
payPriceInt
+
""
;
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
return
"0"
;
}
}
/**
* 获取实付金额,单位:元
*
* @param totalPrice 总价,单位:元
* @param promPrice 优惠价,单位:元
* @return 实付金额,单位:元
*/
public
static
String
getPayPriceYuanStringFromYuan
(
String
totalPrice
,
String
promPrice
)
{
try
{
int
totalPriceInt
=
Integer
.
parseInt
(
transStringYuan2Fen
(
totalPrice
));
int
promPriceInt
=
Integer
.
parseInt
(
transStringYuan2Fen
(
promPrice
));
int
payPriceInt
=
totalPriceInt
-
promPriceInt
;
//检查数字正确性
if
(
totalPriceInt
<
promPriceInt
||
totalPriceInt
<
0
||
promPriceInt
<
0
||
payPriceInt
<
0
)
{
//参数不正确,返回0
return
"0"
;
}
return
transStringPriceToDecimalString
(
payPriceInt
);
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
return
"0"
;
}
}
/**
* 获取实付金额,单位:分
*
* @param totalPrice 总价,单位:分
* @param promPrice 优惠价,单位:分
* @return 实付金额,单位:分
*/
public
static
String
getPayPriceFenStringFromFen
(
String
totalPrice
,
String
promPrice
)
{
try
{
int
totalPriceInt
=
Integer
.
parseInt
(
totalPrice
);
int
promPriceInt
=
Integer
.
parseInt
(
promPrice
);
int
payPriceInt
=
totalPriceInt
-
promPriceInt
;
//检查数字正确性
if
(
totalPriceInt
<
promPriceInt
||
totalPriceInt
<
0
||
promPriceInt
<
0
||
payPriceInt
<
0
)
{
//参数不正确,返回0
return
"0"
;
}
return
payPriceInt
+
""
;
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
return
"0"
;
}
}
}
lib_common/src/main/java/com/fastcashier/lib_common/util/StringPriceFormatUtils.kt
0 → 100644
View file @
81c5d3fb
package
com.fastcashier.lib_common.util
import
java.math.BigDecimal
import
java.math.RoundingMode
import
java.text.DecimalFormat
object
StringPriceFormatUtils
{
/**
* 分转元
*
* @param p
* @return
*/
fun
transStringPriceToDecimalString
(
p
:
String
?):
String
{
val
numberFormat
=
DecimalFormat
(
"0.00"
)
//防止参数不合法异常崩溃
return
try
{
val
price
=
BigDecimal
(
p
)
val
x
=
BigDecimal
(
"100"
)
price
.
divide
(
x
,
2
,
RoundingMode
.
HALF_UP
).
toString
()
}
catch
(
ex
:
Exception
)
{
ex
.
printStackTrace
()
//空或转换异常则显示--.--
"--.--"
}
}
fun
transDoubleStringPriceToDecimalString
(
p
:
String
):
String
{
val
numberFormat
=
DecimalFormat
(
"0.00"
)
//防止参数不合法异常崩溃
return
try
{
val
price
=
p
.
toDouble
()
numberFormat
.
format
(
price
/
100.00
)
}
catch
(
ex
:
Exception
)
{
ex
.
printStackTrace
()
//空或转换异常则显示--.--
"--.--"
}
}
fun
transStringPriceToDecimalString
(
p
:
Double
):
String
{
val
numberFormat
=
DecimalFormat
(
"0.00"
)
//防止参数不合法异常崩溃
return
try
{
numberFormat
.
format
(
p
/
100.00
)
}
catch
(
ex
:
Exception
)
{
ex
.
printStackTrace
()
//空或转换异常则显示--.--
"--.--"
}
}
/**
* 字符元转字符分
*
* @param yuan 字符元
* @return 字符分
*/
fun
transStringYuan2Fen
(
yuan
:
String
):
String
{
var
result
=
yuan
var
tmp1
=
""
var
tmp2
=
""
if
(
yuan
.
contains
(
"."
))
{
val
index
=
yuan
.
indexOf
(
"."
)
tmp1
=
yuan
.
substring
(
0
,
index
)
tmp2
=
yuan
.
substring
(
index
+
1
,
yuan
.
length
)
result
=
yuan
.
replace
(
"."
,
""
)
if
(
tmp2
.
length
<=
0
)
{
result
=
result
+
"00"
}
else
if
(
tmp2
.
length
==
1
)
{
result
=
result
+
"0"
}
else
if
(
tmp2
.
length
>
2
)
{
//如果小数点后有3位或以上说明服务器出现异常,此时仍然只取前两位
result
=
result
.
substring
(
0
,
index
+
2
)
}
}
else
{
result
=
result
+
"00"
}
return
result
}
/**
* 获取实付金额,单位:分
*
* @param totalPrice 总价,单位:元
* @param promPrice 优惠价,单位:元
* @return 实付金额,单位:分
*/
fun
getPayPriceFenStringFromYuan
(
totalPrice
:
String
,
promPrice
:
String
):
String
{
return
try
{
val
totalPriceInt
=
transStringYuan2Fen
(
totalPrice
).
toInt
()
val
promPriceInt
=
transStringYuan2Fen
(
promPrice
).
toInt
()
val
payPriceInt
=
totalPriceInt
-
promPriceInt
//检查数字正确性
if
(
totalPriceInt
<
promPriceInt
||
totalPriceInt
<
0
||
promPriceInt
<
0
||
payPriceInt
<
0
)
{
//参数不正确,返回0
"0"
}
else
payPriceInt
.
toString
()
+
""
}
catch
(
ex
:
Exception
)
{
ex
.
printStackTrace
()
"0"
}
}
/**
* 获取实付金额,单位:元
*
* @param totalPrice 总价,单位:元
* @param promPrice 优惠价,单位:元
* @return 实付金额,单位:元
*/
fun
getPayPriceYuanStringFromYuan
(
totalPrice
:
String
,
promPrice
:
String
):
String
{
return
try
{
val
totalPriceInt
=
transStringYuan2Fen
(
totalPrice
).
toInt
()
val
promPriceInt
=
transStringYuan2Fen
(
promPrice
).
toInt
()
val
payPriceInt
=
totalPriceInt
-
promPriceInt
//检查数字正确性
if
(
totalPriceInt
<
promPriceInt
||
totalPriceInt
<
0
||
promPriceInt
<
0
||
payPriceInt
<
0
)
{
//参数不正确,返回0
"0"
}
else
transStringPriceToDecimalString
(
payPriceInt
.
toDouble
())
}
catch
(
ex
:
Exception
)
{
ex
.
printStackTrace
()
"0"
}
}
/**
* 获取实付金额,单位:分
*
* @param totalPrice 总价,单位:分
* @param promPrice 优惠价,单位:分
* @return 实付金额,单位:分
*/
fun
getPayPriceFenStringFromFen
(
totalPrice
:
String
,
promPrice
:
String
):
String
{
return
try
{
val
totalPriceInt
=
totalPrice
.
toInt
()
val
promPriceInt
=
promPrice
.
toInt
()
val
payPriceInt
=
totalPriceInt
-
promPriceInt
//检查数字正确性
if
(
totalPriceInt
<
promPriceInt
||
totalPriceInt
<
0
||
promPriceInt
<
0
||
payPriceInt
<
0
)
{
//参数不正确,返回0
"0"
}
else
payPriceInt
.
toString
()
+
""
}
catch
(
ex
:
Exception
)
{
ex
.
printStackTrace
()
"0"
}
}
}
\ No newline at end of file
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