Formatting Double into a String in iReport

落爺英雄遲暮 提交于 2019-12-07 23:49:19

问题


I'm doing a report, that I need to join 4 variables in one. If I treat the variables separately I can format them with no problem. But when I merge them into a String, the double value comes as 0.0 instead of 0.00

How can I make it comes as the original, 0.00? The code right now looks like this:

$F{someDoubleField} + "a string" + $F{anotherDoubleField} + "another string"

It prints:

0.0 a string 0.0 another string

instead of:

0.00 a string 0.00 another string

Remember that iReport uses Java, so maybe, Java code can help me out.


回答1:


Use like below:

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



回答2:


Do the next:

  1. Open Properties window for your report (right-mouse click on top node in tree view -> Properties)
  2. Set the Language option to Java
  3. Use such code in your text field expression:

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

    Here

    2 - number of digits after dot
    $V{Sum} - Double or Floaf variable (or field - $F{...})




回答3:


use DecimalFormat

here is howto:

double d = 0.00;

NumberFormat nf = new DecimalFormat("0.00");
String format = nf.format(d);

System.out.println(d); //0.00



回答4:


Perhaps something like:

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



来源:https://stackoverflow.com/questions/4832133/formatting-double-into-a-string-in-ireport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!