不要四舍五入,
如果没有小数位,将小数点后面的.0改成.00(比如2转换成2.00,而不是2.0)
不知道会有多少位小数,有多少位显示多少位
比如这种的
88.495000
将后面的0给去掉
要是88.495236
就不用去掉
如果是六个0就保留小数点后两位
比如
100.000000
改成
100.00
要求:
如何源码不变下增加代码
private Map<String,BigDecimal[]> getMaxMinPrice(List<QuotationOffer> quotbillheads, Map<String,BigDecimal[]> accptanceMap, Map<String,BigDecimal[]> paymentmap,
Map<String,BigDecimal[]> notaxPriceMap, Map<String,BigDecimal[]> notaxAccptanceMap, Map<String,BigDecimal[]> notaxPaymentmap){
Map<String,BigDecimal[]> priceMap = new HashMap<String,BigDecimal[]>();
for(QuotationOffer quot:quotbillheads){
List<QuotationOfferDetail> qutoDetails = quot.getQuotationOfferDetailArrayList();
for(QuotationOfferDetail quotDetail:qutoDetails){
if (quotDetail.getUnpdReleased() != null && quotDetail.getUnpdReleased().booleanValue()) continue;
String offerbid = quotDetail.getOfferBId().toString();
BigDecimal price = quotDetail.getPrice() == null ? BigDecimal.ZERO : quotDetail.getPrice();
BigDecimal acceptancePrice = quotDetail.getAcceptancePrice() == null ? BigDecimal.ZERO : quotDetail.getAcceptancePrice();
BigDecimal paymentPrice = quotDetail.getPaymentPrice() == null ? BigDecimal.ZERO : quotDetail.getPaymentPrice();
BigDecimal notaxPrice = quotDetail.getNoTaxPrice() == null ? BigDecimal.ZERO : quotDetail.getNoTaxPrice();
BigDecimal notaxAcceptancePrice = quotDetail.getNoTaxAcceptancePrice() == null ? BigDecimal.ZERO : quotDetail.getNoTaxAcceptancePrice();
BigDecimal notaxPaymentPrice = quotDetail.getNoTaxPaymentPrice() == null ? BigDecimal.ZERO : quotDetail.getNoTaxPaymentPrice();
BigDecimal price_exchanged = quotDetail.getPrice_exchanged() == null ? BigDecimal.ZERO : quotDetail.getPrice_exchanged();
BigDecimal acceptancePrice_exchanged = quotDetail.getAcceptancePrice_exchanged() == null ? BigDecimal.ZERO : quotDetail.getAcceptancePrice_exchanged();
BigDecimal paymentPrice_exchanged = quotDetail.getPaymentPrice_exchanged() == null ? BigDecimal.ZERO : quotDetail.getPaymentPrice_exchanged();
BigDecimal notaxPrice_exchanged = quotDetail.getNoTaxPrice_exchanged() == null ? BigDecimal.ZERO : quotDetail.getNoTaxPrice_exchanged();
BigDecimal notaxAcceptancePrice_exchanged = quotDetail.getNoTaxAcceptancePrice_exchanged() == null ? BigDecimal.ZERO : quotDetail.getNoTaxAcceptancePrice_exchanged();
BigDecimal notaxPaymentPrice_exchanged = quotDetail.getNoTaxPaymentPrice_exchanged() == null ? BigDecimal.ZERO : quotDetail.getNoTaxPaymentPrice_exchanged();
multMaxMinPrice(priceMap, offerbid, price_exchanged, notaxPrice_exchanged);
multMaxMinPrice(accptanceMap, offerbid, acceptancePrice_exchanged, notaxAcceptancePrice_exchanged);
multMaxMinPrice(paymentmap, offerbid, paymentPrice_exchanged, notaxPaymentPrice_exchanged);
multMaxMinNotaxPrice(notaxPriceMap, offerbid, notaxPrice_exchanged);
multMaxMinNotaxPrice(notaxAccptanceMap, offerbid, notaxAcceptancePrice_exchanged);
multMaxMinNotaxPrice(notaxPaymentmap, offerbid, notaxPaymentPrice_exchanged);
}
}
return priceMap;
}
如果是在改动源码的情况下是什么样的?
public static String wrok(String string){
int i = string.indexOf(".");//寻找小数点
if(i==-1){
//没找到,在后面加上.00直接返回
return string+=".00";
}else{
//找到了
if(string.substring(string.indexOf(".")+1,string.length()).equals("000000")){
//如果小数后面后面六个是0,那么就替换成00
string=string.replace("000000","00");
}else{
//如果最后一位小数是0,去掉零
while (string.lastIndexOf("0")==string.length()-1){
string=string.substring(0,string.lastIndexOf("0"));
}
}
}
//返回结果
return string;
}
加入这个方法,调用即可,望采纳
你自己把这段代码复制出来,自己添加代码,然后调用他,不用人家的这个方法
在不改变源码的情况下,增加代码,只能用动态代理了。
public static String main(String[] args) {
String 值 = "2.0000";
int 尾部零的数量 = new BigDecimal(值).stripTrailingZeros().scale();
if (尾部零的数量 <= 0){
// 尾部没有0
return 值+".00";
} else {
return 值;
}
}
这是C++实现代码,你可以参考一下
#include <iostream>
#include <algorithm>
using namespace std;
string truncate_tail_zeros(const string &str)
{
if (str.find_first_of('.') == string::npos)
return str + ".00";
string r;
auto itr = find_if(str.rbegin(), str.rend(), [](auto c)
{ return c != '0'; });
reverse_copy(itr, str.rend(), back_inserter(r));
if (*itr == '.')
r += "00";
return r;
}
int main()
{
string str;
while (cin >> str)
cout << "=> " << truncate_tail_zeros(str) << endl;
return 0;
}
$ g++ -Wall main.cpp
$ ./a.out
100
=> 100.00
100.0
=> 100.00
100.000
=> 100.00
100.0012345600
=> 100.00123456
public void StrFm(String str){
int tailNum = new BigDecimal(str).stripTrailingZeros().scale();
if (tailNum <= 0){
return str +".00";
} else {
return str;
}
}
在String offerbid = quotDetail.getOfferBId().toString();
添加一段下面的代码,下面处理好了offerbid这个字符串,再接着进行操作
int flag = offerbid.indexOf('.'); // 找到小数点位置
if (flag == -1) // 没找到小数点,说明是整数,直接加.00返回
return offerbid + ".00";
else
offerbid += "00";// 是小数,先补00,后面倒序删除多余的0
int i = offerbid.length() - 1;
while (i > flag + 2 && offerbid.charAt(i) == '0')
i--; // 删除小数点2位后多余的0
offerbid=offerbid.substring(0, i + 1);
加代码,不是可以么?
自己写一个方法判断,然后再这个片段中添加处理即可