【问题标题】:Formatting Double into a String in iReport在 iReport 中将 Double 格式化为字符串
【发布时间】:2011-01-28 19:15:13
【问题描述】:

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

【问题讨论】:

    标签: java formatting jasper-reports double ireport


    【解决方案1】:

    如下使用:

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

    【讨论】:

    • 你没有回答问题
    • 答案是:new DecimalFormat("0.00").format($F{someDoubleField}) + "a string" + new DecimalFormat("0.00").format($F{anotherDoubleField }) + "另一个字符串"
    【解决方案2】:

    做下一个:

    1. 为您的报告打开属性窗口(鼠标右键单击顶部 树视图中的节点 -> 属性)
    2. 语言 选项设置为 Java
    3. 在你的文本字段表达式中使用这样的代码:

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

      这里

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

    【讨论】:

      【解决方案3】:

      使用DecimalFormat

      这里是操作方法:

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

      【讨论】:

        【解决方案4】:

        也许是这样的:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-26
          • 1970-01-01
          • 2023-03-03
          • 1970-01-01
          • 2021-05-05
          • 2010-10-16
          • 2019-06-03
          • 2014-10-15
          相关资源
          最近更新 更多