If you're dealing with doubles or floats

You can simply use

assert xyz == 1.789
xyz.round(1) == 1.8
xyz.round(2) == 1.79
Answer from john Smith on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › home › groovy › groovy round function
Groovy Round Function
March 13, 2016 - class Example { static void main(String[] args){ double d = 100.675; double e = 100.500; println(Math.round(d)); println(Math.round(e)); } }
🌐
Mrhaki
blog.mrhaki.com › 2010 › 01 › groovy-goodness-round-and-truncate.html
Groovy Goodness: Round and Truncate Decimal Values - Messages from mrhaki
November 26, 2010 - Groovy adds round() and truncate() methods to the Double and Float classes. def doubleValue = 12.5456d assert 12.546d == doubleValue.round(3) assert 13 == doubleValue.round() assert 12 == doubleValue.trunc() assert 12.54d == doubleValue.trunc(2) def floatValue = 987.654f assert 987.65f == floatValue.round(2) assert 988 == floatValue.round() assert 987.6f == floatValue.trunc(1) assert 987 == floatValue.trunc() Tags: Groovy Groovy programming Groovy:Goodness GroovyGoodness:Beginner GroovyGoodness:GDK ·
🌐
DZone
dzone.com › coding › languages › groovy goodness: truncate and round bigdecimal values
Groovy Goodness: Truncate And Round BigDecimal Values
June 21, 2018 - def bigDecimal = 42.576135 // Groovy uses BigDecimal for decimal // numbers by default. assert bigDecimal.class.name == 'java.math.BigDecimal' assert bigDecimal.round() == 43 assert bigDecimal.round(2) == 42.58 assert bigDecimal.trunc() == 42 assert bigDecimal.trunc(2) == 42.57
🌐
Apache JIRA
issues.apache.org › jira › browse › GROOVY-3641
[GROOVY-3641] Added new rounding methods to Float and Double that take a precision being the number of decimal places to round to - ASF Jira
Component/s: groovy-jdk · Labels: None · Flags: Patch · Added new rounding methods to Float and Double that take a precision being the number of decimal places to round to. This is a convenience method for Float and Double. Usage: someDouble.round(2) will round someDouble to 2 decimal places.
🌐
Blogger
grailslover.blogspot.com › 2014 › 06 › rounding-off-decimal-value-in-groovy.html
Rounding off decimal value in Groovy ~ Grails lover
Later only I realized that round() is available in double in groovy. I then explicitly converted the variable to double using toDouble() method and chained the round() method to it. It solved my problem. ... So it is evident that the method round() is not available to BigDecimal whereas it is available to Double.
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › decimal-with-groovy
Decimal with Groovy | Binary Encoding Techniques Across Programming Languages
Groovy utilizes Java's BigDecimal class for accurate decimal arithmetic, which is crucial for financial applications and any scenario where floating-point imprecision is unacceptable. ... def price = new BigDecimal("19.99") def taxRate = new BigDecimal("0.08") def taxAmount = price.multiply(taxRate).setScale(2, RoundingMode.HALF_UP) def total = price.add(taxAmount) println "Total: ${total}" // Outputs: Total: 21.59
Find elsewhere
🌐
SAP Community
community.sap.com › t5 › technology-q-a › groovy-code-to-remove-decimal-points › qaq-p › 12552389
Solved: Groovy code to remove decimal points - SAP Community
February 28, 2022 - If you've already registered, sign in. Otherwise, register and sign in. ... If you have a floating point variable in Groovy, you can use the round method to round to a certain number of decimals (5 in your case).
🌐
Groovy
docs.groovy-lang.org › docs › groovy-1.7.2 › html › groovy-jdk › java › lang › Double.html
Double (Groovy JDK)
closure - the code to execute for each number. Since: 1.0 · public long round() Round the value · Returns: the rounded value of that Double · Since: 1.0 · public double round(int precision) Round the value · Parameters: precision - the number of decimal places to keep.
🌐
Groovy
docs.groovy-lang.org › latest › html › groovy-jdk › java › lang › Double.html
Double (Groovy JDK enhancements)
closure - the code to execute for each number · Since: 1.0 · Compares this object against the specified object returning the same result as Double#equals(Object) but returning true if this object and the specified object are both zero and negative zero respectively or vice versa. Parameters: Since: 3.0.8 · Round the value · Returns: the rounded value of that Double · Since: 1.0 · Round the value · Parameters: precision - the number of decimal places to keep ·
🌐
Elvanco Blog
elvanco.com › home › technology › how to use math.round on a big decimal in groovy?
How to Use Math.round on A Big Decimal In Groovy in 2024?
This will round the BigDecimal number stored in bd to 2 decimal places and store the rounded value in the variable roundedNumber. ... The significance of precision when using Math.round in Groovy is that it determines how many decimal places the result will be rounded to.
🌐
SmartBear Community
community.smartbear.com › smartbear community › soapui open source › soapui open source questions
How to round a decimal value when working with context.expand number ...
QUESTION: I need the result value I"m asserting is to = 2.51 · def testValuea = context.expand( '${Properties#a}' ) as int //--11670 def testValueb = context.expand( '${Properties#b}' ) as int //--29250 ... QUESTION: How do I get this to round to (3)?
🌐
Groovy
docs.groovy-lang.org › latest › html › groovy-jdk › java › math › BigDecimal.html
BigDecimal (Groovy JDK enhancements)
a BigDecimal rounded to the number of decimal places specified by precision · Since: 2.5.0 · See Also: BigDecimal#round() BigDecimal#round(java.math.MathContext) Truncate the value · Returns: a BigDecimal truncated to 0 decimal places · Since: 2.5.0 · See Also: BigDecimal#trunc(int) Truncate the value ·
🌐
Mrhaki
blog.mrhaki.com › 2018 › 06 › groovy-goodness-truncate-and-round.html
Groovy Goodness: Truncate And Round BigDecimal Values - Messages from mrhaki
def bigDecimal = 42.576135 // Groovy uses BigDecimal for decimal // numbers by default. assert bigDecimal.class.name == 'java.math.BigDecimal' assert bigDecimal.round() == 43 assert bigDecimal.round(2) == 42.58 assert bigDecimal.trunc() == 42 assert bigDecimal.trunc(2) == 42.57
🌐
Groovy
docs.groovy-lang.org › docs › groovy-1.7.0 › html › groovy-jdk › java › lang › Float.html
Float (Groovy JDK)
the Float rounded to the number of decimal places specified by precision · Since: 1.6.0 · public float trunc(int precision) Truncate the value · Parameters: precision - the number of decimal places to keep. Returns: the Float truncated to the number of decimal places specified by precision ·