Search code examples
grailsgsp

Adding a conditional in gsp file


I have a situation where I get a value as a string and want to do a <= check on it inside my Grails GSP file.

For example.

<g:set var="dueAmount" value="${bean.dueAmount}"/> 
<span class="pay-onetime-btn-wrapper ${dueAmount <=0 ?'show':'hide'}" >bla bla </span> 

I get the following error.

java.lang.Integer cannot be cast to java.lang.String

Which makes sense as the bean.dueAmount is a string. How can I format it as a number or be able to a <= value comparison on it?

Thanks


Solution

  • Looks like your value is a double or floating value. So you can use toDouble() instead. Also, call toString() for the safe side before it.

    <g:set var="dueAmount" value="${bean.dueAmount.toString().toDouble()}" />
    <span class="pay-onetime-btn-wrapper ${dueAmount <=0 ? 'show' : 'hide'}">bla bla</span>