我正在做一份报告,我需要将4个变量合二为一。 如果我单独处理变量,我可以毫无问题地格式化它们。 但是当我将它们合并到一个String中时,double值变为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.00 another string
请记住,iReport使用Java,所以也许,Java代码可以帮助我。
答案 0 :(得分:7)
使用如下:
new DecimalFormat("0.00").format(doubleField) + "str"
答案 1 :(得分:2)
做下一个:
在文本字段表达式中使用此类代码:
String.format("%.2f",$V{Sum}) + " " + $F{unit}
下面
2 - 点之后的位数
$ V {Sum} - Double或Floaf变量(或字段 - $ F {...})
答案 2 :(得分:0)
这是如何:
double d = 0.00;
NumberFormat nf = new DecimalFormat("0.00");
String format = nf.format(d);
System.out.println(d); //0.00
答案 3 :(得分:0)
也许是这样的:
new DecimalFormat(“0.00”)。format(new java.math.Double(($ F {amount} == null)?0:$ F {amount}))