Jump to content
We've recently updated our Privacy Statement, available here ×

formatting question


joannem

Recommended Posts

Hello,

I have a field in my template which contains a text field as well as a decimal field.

Example:   $F{venprice_label} + ": " + $F{venprice}

- venprice_labe needs to be a string variable but venprice needs to be a 2 place decimal field.  Can anyone tell me how to set this in the expression for my field.

Thanks!

Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

There may be an easier way, but this is how I do it. Create a variable called NUM_FORMAT with an initial value of

 

new java.text.DecimalFormat("0.##")

 

Then your expression becomes something like

 

$F{venprice_label} + ": " + $V{NUM_FORMAT}.format($F{venprice}.doubleValue())

 

Alternately with java 1.5/1.6 the following may work (I've never tried it, ireport may not like it)

 

String.format("%s: %.2f", $F{venprice_label}, $F{venprice}.doubleValue())

Link to comment
Share on other sites

I have tried using the NUM_FORMAT option but I get an error. What is the variable class type supposed to be set to for this variable? I left it as string not really thinking about it but then got an error "Cannot cast from Decimal Format to String". I've tried setting it as Double but then get the error only references Double instead of String.

 

Sorry but java is not my strong point so any assistance would be greatly appreciated.

 

Thanks!

Link to comment
Share on other sites

I've attempted and I continue to get casting errors.

 

I've created the variable "$V{NUM_FORMAT}" and put in the initial value "new java.text.DecimalFormat("0.##")". I've tried setting the "Variable Class Type" to Double, String, java.text.DecimalFormat .....

 

My variable that I want to use this format in contains

"( ($F{venprice} != null ) ? $F{venprice_label} + ": " + $V{NUM_FORMAT}.format($F{venprice}.doubleValue()):$F{venprice_label} + ": " + 0.00)"

 

I continue to get errors like the following

1. When I have the Variable Class Type on NUM_FORMAT is set to java.text.DecimalFormat ......

"The method doubleValue() is undefined for the type DecimalFormat"

 

OR

2. When I set the Variable Class Type on NUM_FORMAT to java.lang.Double ..... "Cannot cast from DecimalFormat to Double" as well as the one above "The method doubleValue() is undefined for the type DecimalFormat".

 

So I'm assuming that I need to have the variable NUM_FORMAT set to java.text.DecimalFormat but then how do I correct the error regarding the method doubleValue() ?

 

Thanks for your help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...