在iReport中将Double格式化为字符串

我正在做一个报告,我需要将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代码可以帮助我。

使用如下:

 new DecimalFormat("0.00").format(doubleField) + "str" 

做下一个:

  1. 打开报告的属性窗口(在树视图中右键单击顶部节点 – > 属性
  2. Language选项设置为Java
  3. 在文本字段表达式中使用此类代码:

      String.format(“%。2f”,$ V {Sum})+“”+ $ F {unit} 

    这里

    2 – 点后的位数
    $ V {Sum} – Double或Floaf变量(或字段 – $ F {…})

使用DecimalFormat

这是如何:

 double d = 0.00; NumberFormat nf = new DecimalFormat("0.00"); String format = nf.format(d); System.out.println(d); //0.00 

也许是这样的:

new DecimalFormat(“0.00”)。format(new java.math.Double(($ F {amount} == null)?0:$ F {amount}))