how to set 2 decimal double variable?(URGENT)

i got a problem.
my Double $F{field} could be more than 2 decimal and to display it is no problem as i can use the pattern to format it.
however, i need to sum the double $F{field} and the result should be exactly same as display in each field.
my question is, how can i sum the $F{field} into a variable with exact 2 decimal???
i am currently using ireport 0.5.3
ts_chung's picture
Joined: Jul 18 2006 - 4:30pm
Last seen: 16 years 10 months ago

1 Answer:

If you want consider only the first 2 decimals, of a number, you can multiply the number per 100, take the integer part and divide it again:

Suppose TEST is a Double with a primitive value of 1.2345

you have to sum the following value:

new Double( ((int)($P{TEST}.doubleValue() * 100)) / 100.0 )

If effect if you print this value, you'll get 1.2300
(use 4 decimals in the pattern to be sure)

giulio's picture
Joined: Jan 2 2007 - 4:15pm
Last seen: 2 weeks 5 days ago