views:

243

answers:

2

I am currently building a XML based Jasper Report in which I am trying to add a tax percent onto the cost.

A problem occurs when I use sum(//net-total) to value calculate the total cost. When the summed total has no decimal places it returns and Integer and I need a double.

I have tried sum(//net-total) * 1.0 but it doesn't seem to work.

Does anyone have any ideas?

Please feel free to correct me if I am taking the wrong approach.

+1  A: 

The problem is that by definition (of XML) a double must have a decimal point (.)

[ http://www.w3.org/TR/xmlschema-2/#double ]

You should take a look at the XSLT format-number() Function ..

for example format-number(sum(//net-total), "#.00") would enforce a two decimal digits representation ..

[EDIT]

Since you want it for XPATH 1, which unfortunately does not support conditional expressions nor string formatting , it is not possible to do it in a signle xpath expression ...

if however you can create a node (sum-total) with the sum of the //net-total then you could use a little workaround

concat(
//sum-total,
substring( concat(//sum-total,".00"), 1, number( not(contains(//sum-total,".")))*1000  )
)

which will add .00 if there is no . in the node value ..

Gaby
I tried using the format-number function but it doesn't exist in XPath 1.0 unfortunately.
Gordon
Ticked answer as it fixes the problem also. I was thinking about that approach too.
Gordon
+1  A: 

Found how to set the Object return type in IReport and enforce a pattern on the object. Working fine now.

Gordon
glad you figured it out .. just for the fun of it i posted in my answer above an little trick to do it (with a small requirement)
Gaby