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

variable


tarz87
Go to solution Solved by jgust,

Recommended Posts

I have to create a variable into my report, but this variable was getting from a report created with crystal report. The variable in the crystal report is:

IF (isnumeric(field.string)) then
    ToNumber({field.string})
else
    0

I don't know how I can do this with the jasper report. I must create a variable with java and not with the component in jasper soft.

You can help me!

Thank you

Best regards

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Below is a snipet from the docs which you might find helpful.

Using an If-Else Construct in an Expression

A way to create an if-else-like expression is by using the special question mark operator. For example:

(($F{name}.length() > 50) ? $F{name}.substring(0,50) : $F{name})

The syntax is (<condition>) ? <value on true> : <value on false>. It is extremely useful, and can be recursive, meaning that the value on true and false can be represented by another expression which can be a new condition:

(($F{name}.length() > 50) ?

(($F{name}.startsWidth(“A”)) ? “AAAA” : “BBB”)

:

$F{name})

This expression returns the String AAAA when the value of the field name is longer than 50 characters and starts with A, returns BBB if it is longer than 50 characters but does not start with A, and, finally, returns the original field value if neither of these conditions is true.

Link to comment
Share on other sites

I know the java's statement, but when I put field.isNumeric() in my statement jasper soft ask that is.Numeric() is not valid for the string.

I want to see if my string is numeric or no and I want that gives me is the string is numeric a number otherwise 0. 

I try this statement

field_string..isNaN() == 0 ? 0 : filed.intValue()

or

field_string.isNumeric() == 0 ? 0 : filed.intValue()

but both return error.

You can help me?

Link to comment
Share on other sites

  • Solution

Use the "matchs" method to find if the field is numeric using a regex expression $F{TextMiles}.matches( "^[-+]?\b\d+[\.]?[\d+]" )

After that you can use DOUBLE_VALUE, FLOAT_VALUE, or INTEGER_VALUE built-in functions to convert the value.

Here are the resulting IF expressions.

Double:
$F{TextMiles}.matches( "^[-+]?\b\d+[\.]?[\d+]" ) ? DOUBLE_VALUE( $F{TextMiles} ) : 0

Float:
$F{TextMiles}.matches( "^[-+]?\b\d+[\.]?[\d+]" ) ? FLOAT_VALUE( $F{TextMiles} ) : 0

Integer
$F{TextMiles}.matches( "^[-+]?\b\d+[\.]?[\d+]" ) ? INTEGER_VALUE( $F{TextMiles} ) : 0

 

Link to comment
Share on other sites

  • 2 weeks later...

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...