开发者

How do I round a number in Groovy?

开发者 https://www.devze.com 2023-02-01 13:05 出处:网络
How do I round a number in Groovy? I would like to keep 2 decimal places. For example (pseudo-code): round(1.2334695) = 1.23

How do I round a number in Groovy? I would like to keep 2 decimal places.

For example (pseudo-code):

round(1.2334695) = 1.23
round开发者_如何学Go(1.2686589) = 1.27


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


You can use:

Math.round(x * 100) / 100

If x is a BigDecimal (the default in Groovy), this will be exact.


Use mixin.

class Rounding {
    public BigDecimal round(int n) {
        return setScale(n, BigDecimal.ROUND_HALF_UP);
    }
}

Add this to your startup class and round() is a first-class method of BigDecimal:

BigDecimal.mixin Rounding

Test cases:

assert (new BigDecimal("1.27")) == (new BigDecimal("1.2686589").round(2))
assert (1.2686589).round(2) == 1.27
assert (1.2334695).round(2) == 1.23


Like this:

def f = 1.2334695;
println new DecimalFormat("#.##").format(f);

Or like this:

println f.round (new MathContext(3));

Output:

1.23

Reference: Formatting a Decimal Number


Groovy adds a round() method to the Double and Float classes, so:

(123.456789f).round(2) == 123.46f

Source: Rounding Numbers in Groovy


Probably, more Groovysh way would be to use this snippet (x as double).round(2) like this:

def a = 5.2355434
println "a = $a, a.class = ${a.getClass()}"

def b = (a as double).round(2)
println "b = $b, b.class = ${b.getClass()}"


Looking at @sjtai's and @cdeszaq's answers you don't need to get mixed up with mixin if you just define a method like this:

def bigDecimalRound(n,decimals){
    return(n.setScale(decimals, BigDecimal.ROUND_HALF_UP))
}

It is the BigDecimal builtin method setScale that performs the rounding.

println(1.2334695.setScale(2, BigDecimal.ROUND_HALF_UP))
>> 1.23

It's worth noting also that setScale accepts negative arguments in order to round things to larger order of magnitude, i.e.

println(123.2334695.setScale(-1, BigDecimal.ROUND_HALF_UP))
>> 1.2E+2


Working from @sjtai's great answer, this is the Mixin I use for just about all my decimal rounding needs:

class Rounding {
    public BigDecimal round(int decimalPlaces = 0, RoundingMode roundingMode = RoundingMode.HALF_EVEN) {
        return setScale(decimalPlaces, roundingMode);
    }
}

If rounds to an int by default, and uses an "even" rounding method (reducing statistical error by default is always a good thing), but it still allows the caller to easily override these.


as simple as this:

YOUR_NUMBER = 1.234567
((int) YOUR_NUMBER * 100)/100

note: this would cut off the extra decimal points; it doesn't round up.


For example:

def rd = 1.3425345352
sd = ((float)rd).round(3)

println sd  

>> 1.343


This is surprisingly complex for Groovy. It's usually... groovier.

You need to create a MathContext object to do the rounding.

num =  9.59123331333g
// rounds to 2 places, rounding up by default
mc = new java.math.MathContext(2)
num.round(mc)

==> Result: 7.0

with help from

  • https://blogs.oracle.com/fadevrel/rounding-numbers-and-decimal-places-in-numerical-fields
  • https://docs.oracle.com/javase/7/docs/api/java/math/MathContext.html
  • https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#round(java.math.MathContext)


You can convert any number to float and then use the round() function as: ((float)1.2334695).round(2)

0

精彩评论

暂无评论...
验证码 换一张
取 消