Java Birt 报告中的数字格式为小数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22864183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:30:02  来源:igfitidea点击:

Number format a decimal in Birt reports

javamysqlformatreportbirt

提问by Saurabh Singhal

I have a table which gives me values in Nos. and Decimal (weight-kg) E.g.10.455 Kg ( that is 10 Kg and 455 gms) and nos. as in 10 Nos.

我有一张表,它给出了编号和十进制(重量-公斤)的值,例如 10.455 公斤(即 10 公斤和 455 克)和编号。如10号。

Now all these values come from a column (Decimal (10,3)-mysql) in table and based on the unittype column I have to decide whether to have 3 decimal or zero decimal in the BIRT report.

现在所有这些值都来自表中的一列 (Decimal (10,3)-mysql) 并且基于 unittype 列我必须决定在 BIRT 报告中是有 3 位小数还是 0 位小数。

I know that through scripting the values can be modified.. but I am unable to utilise the scripting.

我知道通过脚本可以修改值..但我无法利用脚本。

I am writing this on onFetch

我在 onFetch 上写这个

if(row["unittype"]=="Nos")
var df = new Packages.java.text.DecimalFormat("#,###.##");
else
var df = new Packages.java.text.DecimalFormat("#,###");

df.format(row["invoicedquantity"]);
this.setDisplayValue(df);

I am unable to get the values

我无法获取值

采纳答案by Dominique

I don't believe this can work from the onFetch script, this kind of script should be put in "onRender" event of a data element. Furthermore, the formatter returns the result so it should be:

我不相信这可以从 onFetch 脚本中工作,这种脚本应该放在数据元素的“onRender”事件中。此外,格式化程序返回结果,因此它应该是:

this.setDisplayValue(df.format(row["invoicedquantity"]));

But i think it would be easier to create a computed column as datatype "String" in the dataset, with expression:

但我认为在数据集中创建一个计算列作为数据类型“字符串”会更容易,表达式:

if(row["unittype"]=="Nos"){
  Formatter.format(row["invoicedquantity"],"#,###.## Kg");
}else{
  Formatter.format(row["invoicedquantity"],"#,### Kg");
}

EDIT:After a deeper look at this, i found a more appropriate way, by changing "numberformat" property. In onRender or onCreate script of the data element (which should be a numberdatatype), we can do something like:

编辑:经过深入研究后,我找到了一种更合适的方法,通过更改“numberformat”属性。在数据元素(应该是数字数据类型)的onRender 或 onCreate 脚本中,我们可以执行以下操作:

if(row["unittype"]=="Nos"){
  this.getStyle().numberFormat="#,###.## Kg";
}else{
  this.getStyle().numberFormat="#,### Kg";
}

This is a better approach because the data element has still a numeric datatype, so that if the report is exported in excel it will be recognized by excel as a number.

这是一种更好的方法,因为数据元素仍然具有数字数据类型,因此如果报表以 excel 导出,excel 会将其识别为数字。