tarz87 Posted March 12, 2020 Share Posted March 12, 2020 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 0I 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 youBest regards Link to comment Share on other sites More sharing options...
luked Posted March 12, 2020 Share Posted March 12, 2020 Below is a snipet from the docs which you might find helpful.Using an If-Else Construct in an ExpressionA 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 More sharing options...
tarz87 Posted March 13, 2020 Author Share Posted March 13, 2020 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 statementfield_string..isNaN() == 0 ? 0 : filed.intValue()orfield_string.isNumeric() == 0 ? 0 : filed.intValue()but both return error.You can help me? Link to comment Share on other sites More sharing options...
Solution jgust Posted March 14, 2020 Solution Share Posted March 14, 2020 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} ) : 0Float:$F{TextMiles}.matches( "^[-+]?\b\d+[\.]?[\d+]" ) ? FLOAT_VALUE( $F{TextMiles} ) : 0Integer$F{TextMiles}.matches( "^[-+]?\b\d+[\.]?[\d+]" ) ? INTEGER_VALUE( $F{TextMiles} ) : 0 Link to comment Share on other sites More sharing options...
tarz87 Posted March 26, 2020 Author Share Posted March 26, 2020 Thank you! It is perfect. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now