Java 在iReport中将Double格式化为字符串
我正在做一个报告,我需要将4个变量合并到一个中。 如果我单独处理变量,我可以毫无问题地格式化它们。 但是当我把它们合并成一个字符串时,双精度值是0.0,而不是0.00 我怎样才能使它成为原来的0.00? 现在的代码如下所示:Java 在iReport中将Double格式化为字符串,java,formatting,jasper-reports,double,ireport,Java,Formatting,Jasper Reports,Double,Ireport,我正在做一个报告,我需要将4个变量合并到一个中。 如果我单独处理变量,我可以毫无问题地格式化它们。 但是当我把它们合并成一个字符串时,双精度值是0.0,而不是0.00 我怎样才能使它成为原来的0.00? 现在的代码如下所示: $F{someDoubleField} + "a string" + $F{anotherDoubleField} + "another string" 它打印: 0.0 a string 0.0 another string 而不是: 0.00 a string 0.
$F{someDoubleField} + "a string" + $F{anotherDoubleField} + "another string"
它打印:
0.0 a string 0.0 another string
而不是:
0.00 a string 0.00 another string
请记住,iReport使用Java,所以Java代码可能可以帮助我解决问题。使用如下方法:
new DecimalFormat("0.00").format(doubleField) + "str"
使用
以下是如何:
double d = 0.00;
NumberFormat nf = new DecimalFormat("0.00");
String format = nf.format(d);
System.out.println(d); //0.00
也许是这样的: 新的DecimalFormat(“0.00”).format(新的java.math.Double($F{amount}==null)?0:$F{amount}))执行以下操作:
2-点后的位数
$V{Sum}-双精度或Floaf变量(或字段-$F{…})
你没有回答这个问题答案应该是:新的十进制格式(“0.00”)。格式($F{someDoubleField})+“一个字符串”+新的十进制格式(“0.00”)。格式($F{anotherDoubleField})+“另一个字符串”