groovy

isCase implementation for BigDecimals needed based on numerical value only

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 1.1-beta-2
  • Component/s: groovy-jdk
  • Labels:
    None
  • Number of attachments :
    1

Description

BigDecimals carry both a numerical value and a precision (scale).

In Groovy, when BigDecimals are compared, only the numerical value is taken into account, but isCase() uses both value and scale.

This should be consistent.

assert 0.5 == 0.50000 // works
switch(0.5) {
case 0.50000: println "match" // doesn't match
}

Activity

Hide
Michael Baehr added a comment -

It looks like a simple method in DefaultGroovyMethods.java is all that is needed:

public static boolean isCase(Number caseValue, Number switchValue) {
return NumberMath.compareTo(caseValue, switchValue)==0;
}

The attached patch contains this method and adds some tests to SwitchWithDifferentTypesTest.groovy.

Show
Michael Baehr added a comment - It looks like a simple method in DefaultGroovyMethods.java is all that is needed: public static boolean isCase(Number caseValue, Number switchValue) { return NumberMath.compareTo(caseValue, switchValue)==0; } The attached patch contains this method and adds some tests to SwitchWithDifferentTypesTest.groovy.
Hide
blackdrag blackdrag added a comment -

applied

Show
blackdrag blackdrag added a comment - applied

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: