Trouble with expressions in Jasper Reports

By: Phil - timelord2004
Trouble with expressions in Jasper Reports
2005-02-07 22:54
(Jasper Reports 0.6.4)
Hi, I've been having some real trouble getting some code running properly within Jasper Reports. I noted (admittedly in the JasperAssistant user guide) the statement "... the possibility of making method calls alone, allows to construct expressions of practically unlimited complexity". While I have had success with a complex single statement, we're having trouble once we try to get a little more clever.
For example, we need to be able to format a currency value and choose the currency symbol dynamically.

1) We started with a textfield and a JasperReports BigDecimal variable, so set the pattern field of the textfield to "$#,###,##0.00", but obviously this is inadequate since the currency symbol is hardcoded.

2) So we tried using a NumberFormat class, but you may also have to tell it the number of decimal places to use, so you end up with:
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
nf.format($V{myValue});
We had all sorts of compile errors. Is it possible to create expressions like this that contain several statements (i.e can you use multiple statements to form a single resultant expression)?

3) Currently, the solution we have is to create an expression:
new DecimalFormat($P{CurrencyFormatString})).format($V{myValue})
(where CurrencyFormatString is for example "?#,###,##0.00")
While this works, it requires the caller of the report to pass in the currency symbol & format string; additionally there's a new DecimalFormat object every time one of these field is rendered, so not so efficient.

4) What we want to use is our own existing 'DataFormat' class in which a static number formatter already exists, and which already already knows about the current currency symbol to use. So the call to our code would be:
DataFormat.floatAsCurrency($V{myValue})
However after after spending hours trying to get the JasperReport to use our DataFormat class we're almost going crazy! We keep getting java compile errors implying 'class not found'.
I know this sounds like a classpath problem, but we've been trying everything to get this going. We've tried compiling the report with JasperAssistant 1.4.5, iReports 0.4.1, and we just can't get either environment to recognise our DataFormat class.
Have tried it in our normal package hierarchy, as a stand-alone file (no package specified), in the root of the class path, in the same directory as the report, etc. The wierd thing is, we've got a custom renderer class for rendering barcodes (using barbeque) and that works fine; we've tried to ensure the DataFormat class is in exactly the same environment as the barbeque renderer, but still no luck.

Does anyone have any tips please? This is very frustrating and we're sure to need similar close integration between the rest of our reports and our application, so this is an important issue for us.

Any ideas, no matter how wacky, are welcome ;-)
Thanks,
Phil





By: Peter Severin - peter_p_s
RE: Trouble with expressions in Jasper Report
2005-02-08 03:21
Hi,

1. There is a special character (? (u00A4) ) that is accepted by DecimalFormat. This character is replaced automatically by the currency symbol that corresponds to the current or preset locale.

2. Expressions do not accept multiple statements. In your case perhaps you could use the NumberFormat.getCurrencyInstance() method. You could get more clever and use the REPORT_LOCALE parameter to get the locale specific format:

NumberFormat.getCurrencyInstance($P{REPORT_LOCALE}).format($V{MyValue})

3. See point 2.

4. JasperAssistant allows you to use classpath setting of an Eclipse Java project (this means you need to use a Java project to host your report). The classpath is configured for each Java project using the "Build Java Path" property page (right click on the project and select Properties). Also make sure that you use a fully qualified class name in your expression. ie. my.package.DataFormat.format($V{myValue})

I hope this helps,

Peter Severin
http://www.jasperassistant.com
JasperAssistant - report designer for JasperReports





By: Phil - timelord2004
RE: Trouble with expressions in Jasper Report
2005-02-08 12:55
Hi Peter, thanks for you responses.

Re. 1: this is a nice idea, but the currency used in the database is not necessarily the same as for the locale. For example the users locale could be set to UK, but the values in the database are all in dollars or euros. This is why we already have a DataFormatter class we use in other parts of the application. Additionally while we currently use only one currency in the database, we may support more than one in the future.

Re. 2: thanks for confirming that we can't use multiple statements :)

Re. 4: Yes I think this is probably an eclipse issue... I'll have to start again and create a new project... hope eclipse doesn't try to screw with our existing 600+ source files ;-)

Thanks again





By: Fabry - fabbro77
RE: Trouble with expressions in Jasper Report
2005-03-24 00:16
You could define a parameter with Class Type java.text.NumberFormat.

Then in your java source code you could get the NumberFormat instance and initialize it.

You assign this instance to your report parameter.

You can use it everywhere to format your numbers

Hi
Fabry
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值