Formatting a string in jaspersoft? - jasper-reports

In my report I have a string field with some numeric value, for example 123102,6. I would like to display 123 102,60 in my report.

Formatting numbers is a bugger in Java in general but gets even trickier with Jasper.
If your report works with a Locale that provides the space as the number group separator (like, for example, Locale.FRANCE does), then all you need to do is specify #,##0.00 as the value of the Pattern property of your field.
If that is not the case, or you are using several Locales, or you are resolving them at runtime, then the simplest way to solve your problem is to provide this:
new DecimalFormat("#,##0.00", DecimalFormatSymbols.getInstance(Locale.FRANCE))
.format(Double.valueOf($F{your_field_name}))
as the value of the Text Field Expression property of your field.

Related

iText DITO - How can I use a fields Data name as a reference in a calculation?

In iText's DITO template designer product, I have one field which has a fairly complex calculation. In another field, I want to use the result of that calculation.
The tooltip says "Identifier used for referencing fields in calculation expressions". So I assumed if I named it blah I can use it in a calculation in another field like this: sum({{blah}},42) (e.g. add 42 to the value of blah). But if I do that I get the error
Unable to resolve reference: "{{blah}}" in node with tag: <span> and text:
The iText DITO documentation does not elaborate on this feature at all. I'm evaluating iText DITO and there does not seem to be any way to get support while evaluating the product.
Suppose I have a field with a very complex calculation. In order to reuse the calculation result, I can do the following:
Give a name to this field (in the image below I've chosen name "total")
Insert a calculation to another field (where I want to reuse "total")
Add "total" as the calculation's body:
That's it, now the total value is reused:
I think the documentation you are looking for is at https://kb.itextpdf.com/home/ditokb/latest/manuals/itext-dito-editor/calculations
You have to give the rich text element a name in the properties to be able to reference it on other rich text element and/or calculations.
See image example at https://kb.itextpdf.com/home/ditokb/files/latest/68620314/68620309/1/1660291029000/image2020-5-27_17-56-41.png

IReport BigDecimal format "R$ #,##0.00" Monetary Value in Charts

I have a JasperReports chart, In the report, the field $F{soma} is BigDecimal, at thedatabase MySQL is Decimal(19,2). I'm using this sql: select SUM(valor) as soma to get the field $F{soma}.
Printing just $F{soma} i get labels like : 1.500,20. Without format expression. What i need is to show labels like : "R$ 1,520.20".
Tried this:
new java.text.DecimalFormat("R$ #,##0.00").format(Double.valueOf($F{soma}))
But no success, so if someone can point me a direction, i'll be thankful.
Have not reputation to post images, but links bellow are about the field types..
Field in MySQL:
Labels being printed (without format expression)
If your $F{soma} is a BigDecimal field then just write
new java.text.DecimalFormat("R$ #,##0.00").format($F{soma});
Use an instance of NumberFormat that has the right Currency to format the value. If the default Locale is giving you a problem,
Use NumberFormat.getCurrencyInstance(Locale inLocale) with the Locale you want, e.g.:
NumberFormat.getCurrencyInstance(Locale.US);
Use NumberFormat.getInstance() and setCurrency() to set the Currency you want, e.g.:
NumberFormat f = NumberFormat.getCurrencyInstance();
f.setCurrency(Currency.getInstance(…));

Override language specific pattern in iReport

How can I override the language specific pattern in iReport? I have set the pattern #,##0.00 on a field with a Double value.
If the report is in english, I get the following example output.:
10,000.00
If the report is in german, I get the following example output.:
10.000,00
I need the output 10.000,00 for the english and the german report.
How can I realize that?
Do not set any pattern (make sure to remove it). Instead, in the TextFieldExpression field use
new java.text.DecimalFormat("#,##0.00", new java.text.DecimalFormatSymbols(java.util.Locale.GERMANY)).format($P{parameter1}).
(This is a string. If your textfield expression class is double, you can parse the result.)
This will format the number as in German locale for all languages.
The latest versions of iReport has a feature called Pattern Expression. In that you only need to specify a pattern string, and leave the rest of the field calculation the same. This way you can separate the data from the format, I think it keeps things cleaner, but it also gives you more control over the format at runtime.
Edit. An example was asked for, here it is:
https://gist.github.com/ilopez/9369809#file-so22141769-jrxml
The magic happens in this expression:
new DecimalFormat().toPattern()
You can specify report locale via the REPORT_LOCALE parameter see: Setting REPORT_LOCALE in IReport?

Return different datatypes crystal formula

I have a requirement where depending on the parameter selected the table field should be displayed as text or date. For eg, if the user selects a true, the OriginDate field which is a datetime type should be displayed as a date in the report else should be displayed as a text. I tried creating a formula an doing something like below, and then placing the formula in the details section of the report but it doesnt work due to different datatypes returned.
if {?Mailmerge}=true then
ToText({Travel.OriginDatetime}, "M/d/yy")
else
{Travel.OriginDatetime}
Is there a way I can accompalish the above requirement so that I do not end up creating 2 reports one with field displayed as text and other as date?
Try creating a formula that returns your field as a string totext({Travel.OriginDateTime},"M/d/yy") and overlay it with the original field {Travel.OriginDateTime} and conditionally suppress them based on the value of {?MailMerge} such that only one shows in any one instance of the report.
If I'm following you, you want to only show M/d/yy when ?MailMerge is true, otherwise yuo want to show the full date?
You can't show different datatypes in the same formula, so just convert them both parts of the conditional statement into strings:
if {?Mailmerge}=true then
ToText({Travel.OriginDatetime}, "M/d/yy") // 8/30/12
else
ToText({Travel.OriginDatetime}, "M/d/yy hh:mm:ss") // 8/30/12 02:06:00
I don't know what exactly your dates look like in the database, but you can use the above as a guideline into formatting your date based on the ?MailMerge parameter.

JasperReports: default value instead of 'null'

Is there any way to set a default value to a field in a report? I have a lot of String fields in a report and would like them to display "0,00" when they're null.
Supposing the field name is "value", in the "Text Field Expression", write:
($F{value} != null) ? $F{value} : "0.00"
You can also select "Blank when null" in the properties of the text field if you want that. Other options are more flexible but this does the trick very quick and easy.
medopal's answer is good, but 2 additions:
1) You can make the syntax shorter:
($F{field_name}) ? $F{field_name} : "0.00"
2) Make sure your "else" data is of the same class as the field's value, otherwise you'll get errors when it tries to coerce numbers into string, etc. etc. This was something that, as I started out, I mixed up.
Did you try set a pattern in the text field?
If you are using iReport this can be found in the properties for the text field in the Text Field Properties section.
Try something along the lines of ###0.00 to represent 1234.56, which would always display 0.00 even if it is null.
This is the easiest way is to use Coalesce() or NVL() function of database in your data-source query to restrict null data on your report.
But it depends on if you are allowed to change the datasource query or not. If not then you can go for other solutions provided in previous answers.