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

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.

我正在做一个报告,我需要将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.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}))

执行以下操作:

  • 打开报告的属性窗口(鼠标右键单击顶部 树状视图中的节点->属性
  • Language选项设置为Java
  • 在文本字段表达式中使用以下代码:

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

    2-点后的位数
    $V{Sum}-双精度或Floaf变量(或字段-$F{…})


  • 你没有回答这个问题答案应该是:新的十进制格式(“0.00”)。格式($F{someDoubleField})+“一个字符串”+新的十进制格式(“0.00”)。格式($F{anotherDoubleField})+“另一个字符串”