vhbjknkm 2022-02-12 15:00 采纳率: 100%
浏览 252
已结题

字符串如何截取小数点后面0的方法

要求:

不要四舍五入,
如果没有小数位,将小数点后面的.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;
    }

如果是在改动源码的情况下是什么样的?

  • 写回答

8条回答 默认 最新

  • 梦寒君 2022-02-14 14:44
    关注
    
    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;
        }
    

    加入这个方法,调用即可,望采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

问题事件

  • 系统已结题 2月23日
  • 已采纳回答 2月15日
  • 修改了问题 2月12日
  • 修改了问题 2月12日
  • 展开全部

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图