Skip to content
Advertisement

formatting a string to a currency format in jasper report

I have a string with some numeric value.

I want to format it in a way where hundreds are comma separated and the number is having $ dollar sign before it.

e.g. 12345 should be formatted to $ 12,345.00

I tried the below code without dollar sign:

JavaScript

and the below one with dollar sign:

JavaScript

However, both are giving error.

What is the right way to achieve this format ?

This is a part of jasper report jrxml where I want to avoid “null” on the report and thus inserting the below code:

JavaScript

Where myString results from a query and is declared in jrxml as:

JavaScript

Earlier myString was declared as BigDecimal, but then comparison operator ?= was not working.

If the currency value is not available, I want to print “unavailable” on the report instead of default “null”. Else, I want the number to be properly formatted as described above.

How to resolve this issue?

Thanks for reading.

Advertisement

Answer

The correct expression is:

JavaScript

The working sample for java.lang.Integer and java.lang.String:

JavaScript

The result will be (preview in iReport):

The result in *iReport*

Note: You should also add check for null.

You can also use pattern property of textField for formatting data.

The sample:

JavaScript

The result will be the same.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement