iReport sum of two measures - jasper-reports

I'm having problem with JasperReports report by getting sum of two fields.
I have created so far variables that holds fields "TareWeight" variable that is casting values from double to float
new Float($F{EquipmentTareWeightKg})
"CargoWt" variable that is casting from string to float, etc...
Float.parseFloat($F{UfvFlexString03})+Float.parseFloat($F{UfvFlexString04})
So now I have two variables which I can manipulate with.
The problem starts when I wish to do a "recap" using crosstab. I have setup two groups that is done with wizard, and added another in crosstab. (please refer to the printscreen below)
And now, I have created under measures SumOf Tare that is using variable TareWt, and SumOfCargoWt that is using variable CargoWt.
I have tried to create another Measure SumOfGrossWt that would do a sum of variables TareWt + CargoWt but I have got some nonsense values.
I have tried to do another approach by creating variable GrossWt that would basically do exact the same thing as previous attempt
$V{Tare}+$V{CargoWt}
or use an variable: GrossWt
$V{Tare}+$V{CargoWt} or new Float($F{EquipmentTareWeightKg})+(Float.parseFloat($F{UfvFlexString03})+Float.parseFloat($F{UfvFlexString04}))
None of these actually worked: (Sum should be done by "Sum Of Tare"+ "Sum of CargoWt" = "Sum of GrossWt") plese refer to the second picture.
Here is a complete jrxml code/file:
jrxml file
save it as *.jrxml
Can please someone help me find a solution? how can I sum those two measure fields and get correct values?

Since CargoWt field had some null values in database, addition float value with null is equal to null, so the only value which was displayed on report are the one that had values for CargoWt, all others with null value were "ignored".

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

How to do conditional sum in Crystal Report

Question originally posted in Spanish, on es.stackoverflow.com, by Sebastián Miranda:
I am working with Crystal Report since Visual Studio 2017, I was asked
to make a sum to group, I have 3 options: Credit Notes, Ballot and
Invoice.
The first thing I did was to create a Formula Field with the name
NumberValorCif to convert the ICIF Value that I have to numeric with ToNumber (). I saved this and created another formula field
named SumCIFCredito that has the following code:
If {CobroPorcentaje.TipoDoc} = 'Credit Notes'
then {#NumberValorCif}
else 0
I saved, compiled and ran the application and it worked without any
problems ... but. I wanted to do the same for SumCIFBoleta:
If {CobroPorcentaje.TipoDoc} = 'Ballot'
then {#NumberValorCif}
else 0
But I don't add anything. I'm missing something? or I have to create
another variable.
I hope someone can help me.
Thank you so much
It sounds like you simply need to add the actual SUM of that new formula.
Place the new formula in the detail section, select it, hit the Sigma button to sum it, and elect to generate subtotals for your Grouping level.

Mixing lod and non-lod calculations

I have a data set that looks like this:
I want to use a parameter to select a value from Group and then compute Ratio which is Value divided by Value when Group == parameter. The image above is the desired output when the parameter equals D.
I thought that this would do the trick:
[Value]/{EXCLUDE [GROUP]: MAX(VALUE)}
But I just get nulls.
Thanks for your help!
LOD expressions dont work that way and they will always show as null values in the datasource page. Here is the Documentation. (Bullet point 2)
For your case, create the parameter as shown below. (You can use 'Paste from clipboard' as by default only values can be assigned by field and not 'Display As'.)
Now your calculation simply becomes
[Value]/[Selected_Group]

Round up double value in jasper report

I have a textfield in jasper report as follows :
<textFieldExpression class="java.lang.Double"><![CDATA[$V{v_vcTotal}]]></textFieldExpression>
I want to round up this value using HALF_UP rounding mode.Can i achieve this in a simple way.Thanks in advance.
EDIT : I dont want to change my java class. Is there anything i can do in jasper jrxml file.
So all credit to #utkarsh their expression is so far the only working one I've seen online that converted my 'double' numbers pulled onto my report, my expression:
$f{balance}
Would produce doubles from a calculated field in my database like "-1365.00000000003"
In the Expression editor (in lieu of changing it in the source tab) I wanted to pull 2 sig figs from the number for looks. I added this expression and I stopped getting "compilation" errors and the wanted results:
new BigDecimal($F{balance}).setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()
Result would be "-1365.00"
From what I've read this is a bit overkill to get the desired result but I am also under a deadline so I will try to optimize some other time.

iReport subreport return value

I am using iReport 4.0.2 and I want to show a result in my main report. For example, in my main report we have two columns and I want to get the sum of that two columns, just like this format:
A B sum
10 5 15
Where A is one field in the main report and B is a return value of my subreport.
This works well. But, the key point is that sometimes subreport will not return any value, which is the problem. In this case, the result of sum is like this:
A B sum
10 NULL
As we see here, B is the subreport return value but it's value neither NULL nor 0 . That's why we have that problem.
I try to find how can I get the return value from subreport when the SQL returns no results. I know iReport has a property named 'When No Data', but it doesn't help.
So I want to know, whether we have another way to solve the problem in iReport or using some SQL skills.
I think that in your second example B is actually null but perhaps your text field has the 'Blank When Null' property set. I was able to reproduce your results except for the blank for B.
The real issue is that you can't add or subtract with a null value. JasperReports will just print null. You have to add some logic to provide a default value if one of the variables in the expression is null. For example
$F{MAIN_REPORT_FIELD} - ($V{SUB_RESULT} == null ? 0 : $V{SUB_RESULT})
Unfortunately, this is broken for reports that use Groovy for expressions in JasperReports 4.0.2 (Case 0005138) and will always return null.
You have a few options:
You could upgrade to JasperReports/iReport 4.1.1 which resolves this issue.
You could switch to using Java for expressions.
Depending on your report this will either be painless or it will be a chore. Any fields that don't have the correct Expression Class will have to be fixed up and you'll probably have to be more explicit in any expression that performs operations on non primitive/wrapper types.
if you are doing sum of main field and sub report field in main report then in the sum expression (main report field + (subreportfield==null ? 0 : subreportfield) .
return subreport values to main report variable.
Then do the sum of the main report field and bareport return variable.
Do the same condition as given above.