-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NU-1932] Big decimal uses default scale (#7356)
Co-authored-by: Pawel Czajka <[email protected]>
- Loading branch information
Showing
9 changed files
with
192 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...utils/src/main/java/pl/touk/nussknacker/springframework/util/BigDecimalScaleEnsurer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package pl.touk.nussknacker.springframework.util | ||
|
||
import java.math.RoundingMode; | ||
|
||
/* | ||
In spel division of two big decimals uses divide operator org.springframework.expression.spel.ast.OpDivide. | ||
In this operator if one of the divided numbers is BigDecimal then second number is also converted to BigDecimal using org.springframework.util.NumberUtils class | ||
and resulting BigDecimal will have scale (that is number of stored digits to the right of decimal point) equal to max of the scales of two divided numbers. | ||
The behaviour of org.springframework.util.NumberUtils is to convert integers to BigDecimals in such a way, that they have scale equal to 0. | ||
This may lead to unexpected behaviour. For instance | ||
"(1).toBigDecimal / 2" | ||
would evaluate to BigDecimal("0") (we use org.springframework.util.NumberUtils to convert 1 to BigDecimal). | ||
This is not an acceptable result because | ||
"(1).toDouble / 2" | ||
will always evaluate to "0.5" and user would expect higher computation precision from BigDecimal. | ||
To avoid such issues we use this class to make sure that BigDecimals which are created always have scale of at least DEFAULT_BIG_DECIMAL_SCALE. | ||
BigDecimals can be created in org.springframework.util.NumberUtils conversion, or in our .toBigDecimal spel extension. | ||
There is the risk that big decimals enter process in other ways (for instance from jdbc controllers or from scenario input), | ||
and they may have small scales. This may again lead to unexpected | ||
behaviour when using division operator. This issue can be solved by using our own version of OpDivide class, but for now we decided | ||
not to do it. | ||
*/ | ||
object BigDecimalScaleEnsurer { | ||
// visible for testing | ||
val DEFAULT_BIG_DECIMAL_SCALE = 18 | ||
|
||
def ensureBigDecimalScale(value: java.math.BigDecimal): java.math.BigDecimal = { | ||
value.setScale(Math.max(value.scale(), BigDecimalScaleEnsurer.DEFAULT_BIG_DECIMAL_SCALE), RoundingMode.UNNECESSARY) | ||
} | ||
} |