JSTL标签的formatNumber 格式化数字,四舍六入问题。

8 篇文章 0 订阅

1、在工作当中,我们遇到了一个问题,那就是当我们用JSTL标签的formatNumber 格式化数字时,如

<fmt:formatNumber value="${mln:getFs(list[10])}" pattern="#0.00"/>

 

2、默认会四舍六入五奇偶,和我们导出的弼杰报表不一致,弼杰逢五进一:

    如界面是27.14,弼杰报表是27.15,真实数据是27.145。

 

3、这时候我们要统一算法,必须让formatNumber 处理方式和弼杰一样,也就是不进行入,相当于改为逢五进一。

 

4、我通过跟踪formatNumber 标签,最终在FormatNumberSupport类中找到了处理方法,原方法如下

  public int doEndTag()
    throws JspException
  {
    String formatted = null;
    Object input = null;

    if (this.valueSpecified)
    {
      input = this.value;
    }
    else if ((this.bodyContent != null) && (this.bodyContent.getString() != null)) {
      input = this.bodyContent.getString().trim();
    }

    if ((input == null) || (input.equals("")))
    {
      if (this.var != null) {
        this.pageContext.removeAttribute(this.var, this.scope);
      }
      return 6;
    }

    if ((input instanceof String)) {
      try {
        if (((String)input).indexOf('.') != -1)
          input = Double.valueOf((String)input);
        else
          input = Long.valueOf((String)input);
      }
      catch (NumberFormatException nfe) {
        throw new JspException(Resources.getMessage("FORMAT_NUMBER_PARSE_ERROR", input), nfe);
      }

    }

    Locale loc = SetLocaleSupport.getFormattingLocale(this.pageContext, this, true, NumberFormat.getAvailableLocales());

    if (loc != null)
    {
      NumberFormat formatter = null;
      if ((this.pattern != null) && (!this.pattern.equals("")))
      {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols(loc);
        formatter = new DecimalFormat(this.pattern, symbols);
      } else {
        formatter = createFormatter(loc);
      }
      if (((this.pattern != null) && (!this.pattern.equals(""))) || ("currency".equalsIgnoreCase(this.type))) {
        try
        {
          setCurrency(formatter);
        } catch (Exception e) {
          throw new JspException(Resources.getMessage("FORMAT_NUMBER_CURRENCY_ERROR"), e);
        }

      }

      configureFormatter(formatter);
      formatted = formatter.format(input);
    }
    else {
      formatted = input.toString();
    }

    if (this.var != null)
      this.pageContext.setAttribute(this.var, formatted, this.scope);
    else {
      try {
        this.pageContext.getOut().print(formatted);
      } catch (IOException ioe) {
        throw new JspTagException(ioe.toString(), ioe);
      }
    }

    return 6;
  }

 修改后处理方式如下

 

  public int doEndTag()
    throws JspException
  {
    String formatted = null;
    Object input = null;

    if (this.valueSpecified)
    {
      input = this.value;
    }
    else if ((this.bodyContent != null) && (this.bodyContent.getString() != null)) {
      input = this.bodyContent.getString().trim();
    }

    if ((input == null) || (input.equals("")))
    {
      if (this.var != null) {
        this.pageContext.removeAttribute(this.var, this.scope);
      }
      return 6;
    }

    if ((input instanceof String)) {
      try {
        if (((String)input).indexOf('.') != -1)
          input = Double.valueOf((String)input);
        else
          input = Long.valueOf((String)input);
      }
      catch (NumberFormatException nfe) {
        throw new JspException(Resources.getMessage("FORMAT_NUMBER_PARSE_ERROR", input), nfe);
      }

    }

    Locale loc = SetLocaleSupport.getFormattingLocale(this.pageContext, this, true, NumberFormat.getAvailableLocales());

    if (loc != null)
    {
      NumberFormat formatter = null;
      if ((this.pattern != null) && (!this.pattern.equals("")))
      {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols(loc);
        formatter = new DecimalFormat(this.pattern, symbols);
        int i = 0;
        StringBuffer str=new StringBuffer("");
        
        if((i = this.pattern.indexOf(".")) >0){
        	i = this.pattern.substring(i+1).length();
        	for(int j=i;j>0;j--){//组装遇到27.005 #0.00需要进位的情况补充值,即给27.005+0.001,格式化的时候遇到6就整体入一位结果为27.01
        		str.append("0");
        	}
        	if ((input instanceof BigDecimal)){
        		//pattern #0.00 #0.000 #0.0000
        		String inputstr = ((BigDecimal)input).toString();//传入的字符串
        		int strint = inputstr.indexOf('.');
        		if(strint != -1){
	        		inputstr = inputstr.substring(strint+1);//从.的位置开始截取生产小数位字符串
	    		    if(inputstr.length() > i){//如果小数位字符串长度大于需要格式化的长度
	    			  String number = inputstr.substring(i, i+1);//得到需要四舍五入的最后一位数字,如果是五就加一
	    			  if(number.equals("5")){
	    	        		BigDecimal a = (BigDecimal)input;
	    	        		BigDecimal b = new BigDecimal("0."+str.toString()+"1"); //组装位数的后一位加一
	    	        		input = a.add(b);        
	    			  }
	    		    }
        		}
        	}else if((input instanceof Double)){
        		//pattern #0.00 #0.000 #0.0000
        		String inputstr = Double.toString((Double)input);//传入的字符串
        		int strint = inputstr.indexOf('.');
        		if(strint != -1){
        		    inputstr = inputstr.substring(strint+1);//从.的位置开始截取生产小数位字符串
	    		    if(inputstr.length() > i){//如果小数位字符串长度大于需要格式化的长度
	    			  String number = inputstr.substring(i, i+1);//得到需要四舍五入的最后一位数字,如果是五就加一
	    			  if(number.equals("5")){
	    				  Double a = (Double)input;
	    				  double b = Double.parseDouble("0."+str.toString()+"1");
	    	        	  input = a+b;        
	    			  }
	    		    }
    		    }
        	}
        }
      } else {
        formatter = createFormatter(loc);
      }
      if (((this.pattern != null) && (!this.pattern.equals(""))) || ("currency".equalsIgnoreCase(this.type))) {
        try
        {
          setCurrency(formatter);
        } catch (Exception e) {
          throw new JspException(Resources.getMessage("FORMAT_NUMBER_CURRENCY_ERROR"), e);
        }
      }
      configureFormatter(formatter); 
      formatted = formatter.format(input);
    }
    else {
      formatted = input.toString();
    }

    if (this.var != null)
      this.pageContext.setAttribute(this.var, formatted, this.scope);
    else {
      try {
        this.pageContext.getOut().print(formatted);
      } catch (IOException ioe) {
        throw new JspTagException(ioe.toString(), ioe);
      }
    }

    return 6;
  }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值