JSTL标签的formatNumber格式化数字,四舍六⼊问题。
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) && (String() != null)) {
input = String().trim();
}
if ((input == null) || (input.equals("")))
{
if (this.var != null) {
veAttribute(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);
input = Long.valueOf((String)input);
}
catch (NumberFormatException nfe) {
throw new Message("FORMAT_NUMBER_PARSE_ERROR", input), nfe);
}
}
Locale loc = FormattingLocale(this.pageContext, this, true, AvailableLocales());
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".pe))) {
try
bigdecimal格式化两位小数
{
setCurrency(formatter);
} catch (Exception e) {
throw new Message("FORMAT_NUMBER_CURRENCY_ERROR"), e);
}
}
configureFormatter(formatter);
formatted = formatter.format(input);
}
else {
formatted = String();
}
if (this.var != null)
this.pageContext.setAttribute(this.var, formatted, this.scope);
else {
try {
Out().print(formatted);
} catch (IOException ioe) {
throw new String(), 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) && (String() != null)) {
else if ((this.bodyContent != null) && (String() != null)) {
input = String().trim();
}
if ((input == null) || (input.equals("")))
{
if (this.var != null) {
veAttribute(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 Message("FORMAT_NUMBER_PARSE_ERROR", input), nfe);
}
}
Locale loc = FormattingLocale(this.pageContext, this, true, AvailableLocales());
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."+String()+"1"); //组装位数的后⼀位加⼀
input = a.add(b);
}
}
}
}else if((input instanceof Double)){
//pattern #0.00 #0.000 #0.0000
String inputstr = String((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")){
if(number.equals("5")){
Double a = (Double)input;
double b = Double.parseDouble("0."+String()+"1");
input = a+b;
}
}
}
}
}
} else {
formatter = createFormatter(loc);
}
if (((this.pattern != null) && (!this.pattern.equals(""))) || ("currency".pe))) {
try
{
setCurrency(formatter);
} catch (Exception e) {
throw new Message("FORMAT_NUMBER_CURRENCY_ERROR"), e);        }
}
configureFormatter(formatter);
formatted = formatter.format(input);
}
else {
formatted = String();
}
if (this.var != null)
this.pageContext.setAttribute(this.var, formatted, this.scope);
else {
try {
Out().print(formatted);
} catch (IOException ioe) {
throw new String(), ioe);
}
}
return 6;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。