Converting a string to int in Groovy

前端 未结 13 2242
陌清茗
陌清茗 2020-11-30 18:45

I have a String that represents an integer value and would like to convert it to an int. Is there a groovy equivalent of Java\'s Integer.pars

相关标签:
13条回答
  • 2020-11-30 19:04

    As an addendum to Don's answer, not only does groovy add a .toInteger() method to Strings, it also adds toBigDecimal(), toBigInteger(), toBoolean(), toCharacter(), toDouble(), toFloat(), toList(), and toLong().

    In the same vein, groovy also adds is* eqivalents to all of those that return true if the String in question can be parsed into the format in question.

    The relevant GDK page is here.

    0 讨论(0)
  • 2020-11-30 19:04

    The Simpler Way Of Converting A String To Integer In Groovy Is As Follows...

    String aa="25"
    int i= aa.toInteger()
    

    Now "i" Holds The Integer Value.

    0 讨论(0)
  • 2020-11-30 19:10

    also you can make static import

    import static java.lang.Integer.parseInt as asInteger
    

    and after this use

    String s = "99"
    asInteger(s)
    
    0 讨论(0)
  • 2020-11-30 19:10

    Several ways to achieve this. Examples are as below

    a. return "22".toInteger()
    b. if("22".isInteger()) return "22".toInteger()
    c. return "22" as Integer()
    d. return Integer.parseInt("22")
    

    Hope this helps

    0 讨论(0)
  • 2020-11-30 19:12

    toInteger() method is available in groovy, you could use that.

    0 讨论(0)
  • 2020-11-30 19:13

    Groovy Style conversion:

    Integer num = '589' as Integer
    

    If you have request parameter:

    Integer age = params.int('age')
    
    0 讨论(0)
提交回复
热议问题