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 OverflowTutorialsPoint
tutorialspoint.com › home › groovy › groovy round function
Groovy - round() method
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)); } }
How to use Math.round on a big deciaml in groovy? - Stack Overflow
I have a variable in my groovy script that collects a cost value. However the value comes out as 91.7776766667 and I want it to be rounded up so it is displayed as 91.8. How can I change the code b... More on stackoverflow.com
jmeter - Groovy: How to define float numbers that would generate output as 2 digit decimal points - Stack Overflow
Algo is to generate random numbers but in form of 2 decimal points like 4.78, 3.88, etc How can we achieve this? More on stackoverflow.com
java - How to round the double value to 2 decimal points? - Stack Overflow
Possible Duplicate: round double to two decimal places in java I want to round up the double value upto 2 decimal points. for example: I have double d=2; and the result should be result =2.00 More on stackoverflow.com
Some f32 numbers refuse to round to decimal places properly.
The number 94.870 cannot be exactly represented as a float. The number you got is the closest float to that value. What do you actually need? A string with two decimal places? You can do that using the {:.02} formatter. fn two_decimals(fl: f32) -> String { format!("{:.02}", fl) } If you need exact arithmetic for things like money, then store the number of cents as an integer. More on reddit.com
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 ·
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.
Top answer 1 of 2
1
Instead of Math.round() you can use BigDecimal.setScale(scale, roundingMode) on your number directly.
def testcost = jsongroups.options.cost.flatten().collect { new BigDecimal (it).setScale(1, RoundingMode.HALF_UP)}
log.info testcost
HALF_UP will round .01-.04 to .0 and .05-.09 to .1. Read the JavaDoc of RoundingMode for other rounding possibilities, like HALF_EVEN etc.
2 of 2
-1
Groovy language has a round() method which going to round your decimal number to specific decimal place as a parameter.
def decimalValue = (Double)91.7776766667
println "Value upto two decimal places >>"+decimalValue.round(2)
Output:
Value upto two decimal places >>91.78
Pricefx
developer.pricefx.eu › pricefx-api › groovy › library › SharedLib › elements › RoundingUtils.html
RoundingUtils (Groovy Documentation)
Round a percentage to 2 decimals or upon based on a rounding lookup table name if provided.
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
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 ·
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.
Boomi Community
community.boomi.com › s › question › 0D51W00006As148SAB › rounding-in-groovy
Question: Rounding in groovy - Boomi Community
Loading · ×Sorry to interrupt · Refresh
Top answer 1 of 2
1
You can round a float value using DecimalFormat class like:
new java.text.DecimalFormat('#.##').format(yourFloat)
Demo:

Check out Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter
2 of 2
1
If you return a BigDecimal, you can tell it how many digits it handles. E.g.:
println(new BigDecimal(0.666).setScale(2, java.math.RoundingMode.HALF_UP))
// => 0.67
Only do this at the very end of your calculations.
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 values? | SmartBear Community
May 24, 2019 - 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 ·
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 ·