Skip to content

Built in converters

zhaber edited this page Aug 2, 2012 · 10 revisions

Base type converters:

  • toBigDecimal (uses current locale)
  • toBoolean (returns a boolean value according to the Groovy Truth)
  • toChar
  • toEnum
  • toDouble
  • toFloat
  • toInt
  • toLong
  • toList, where is any type that has a predefined converter

Other type converters:

  • toNaturalInt (convert value to a non-negative integer number)
  • toNaturalLong
  • toNonnegativeBigDecimal
  • toPositiveInt
  • toPositiveLong
  • toPositiveBigDecimal
  • likewise, for float and double types Date-time converters:

toDate(String value, String pattern, Locale locale = Locale.default) The converter tests a date against the given date format using the default locale and time zone and creates a java.util.Date instance. A usage example:

toDate(‘yyyyMMdd’)

toDate(‘MMMM, yyyy’)

The pattern parameter specifies the order in which the year, month and day values are passed (see the java.text.SimpleDateFormat class for the list of supported patterns). The converter can be used to process HTML5 input of type datetime.

Other converters:

Function name

Action taken

abs(Integer/Long/Float/Double value)

returns the absolute value of a int, long, float, or double value.

add(Number value, Number number)

adds number to the value (same as {it + number})

capitalize(String value)

capitalizes the first letter

ceil(Number value)

rounds fractions up

collect(List values, Closure transform)

transforms each list member into a new value using the transform closure

decimalFormat(String value, String pattern)

calls DecimalFormat.applyPattern with the value and the pattern

div(Number value, Number number)

divides the value by the given number

floor(Number value)

rounds fractions down

format(String value, List args)

formats the value using the specified format string and arguments, see String.format(String format, Object... args) for more details [97]

mod(Long value, Long number)

returns the value modulo number

minus(Number value, Number number)

subtracts number from the value (same as {it + number})

mult(Number value, Number number)

multiplies the value by number

replaceAll(String value, String regexp, String replacement)

replaces each substring of the value that matches the given regular expression with the given replacement

round(Number value)

rounds to the nearest integer

setLowerLimit(Number value, Number min)

substitutes the value with min if it is less than min 

setUpperLimit(Number value, Number max)

substitutes the value with max if it is greater than max 

substring(String value, int beginIndex, int endIndex = -1)

converts to a new string that is a substring of the value; the substring begins at the specified beginIndex and extends to the character at index endIndex - 1

toLowerCase(String value)

converts the value to lowercase (using the default locale)

toUpperCase(String value)

converts the value to uppercase (using the default locale)