-
Notifications
You must be signed in to change notification settings - Fork 7
Remove BigDecimal from HighPrecisionMoney.fromPreciseAmount #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.sphere.util | ||
|
||
import java.util.Currency | ||
import scala.annotation.tailrec | ||
|
||
/** This object contains rounding algorithms for the Money classes. So far we used BigDecimal for | ||
* this purpose, but BigDecimal is slower and consumes more memory than this approach. | ||
*/ | ||
object MoneyRounding { | ||
|
||
private def pow10(n: Int): Long = Math.pow(10, n).toLong | ||
|
||
/** @return | ||
* Floor rounded (preciseAmount, fractionDigits) to the cent value of the given currency | ||
*/ | ||
def roundFloor(preciseAmount: Long, fractionDigits: Int, currency: Currency): Long = | ||
if (preciseAmount < 0L) { | ||
val power = pow10(fractionDigits - currency.getDefaultFractionDigits) | ||
val floor = preciseAmount / power | ||
val remainder = preciseAmount % power | ||
if (remainder == 0L) floor else floor - 1L | ||
} else | ||
preciseAmount / pow10(fractionDigits - currency.getDefaultFractionDigits) | ||
|
||
/** @return | ||
* Ceiling rounded (preciseAmount, fractionDigits) to the cent value of the given currency | ||
*/ | ||
def roundCeiling(preciseAmount: Long, fractionDigits: Int, currency: Currency): Long = | ||
if (preciseAmount < 0L) | ||
preciseAmount / pow10(fractionDigits - currency.getDefaultFractionDigits) | ||
else { | ||
val power = pow10(fractionDigits - currency.getDefaultFractionDigits) | ||
val floor = preciseAmount / power | ||
val remainder = preciseAmount % power | ||
if (remainder == 0L) floor else floor + 1L | ||
} | ||
|
||
private def getFractionDigits( | ||
fractionWithoutLeadingZeros: Long, | ||
fractionDigits: Int): List[Int] = { | ||
@tailrec | ||
def loop(remainder: Long, acc: List[Int]): List[Int] = { | ||
val lastDigit = (remainder % 10L).toInt | ||
val newRemainder = remainder / 10L | ||
val newAcc = lastDigit :: acc | ||
if (newRemainder == 0L) newAcc | ||
else loop(newRemainder, newAcc) | ||
} | ||
val digits = loop(fractionWithoutLeadingZeros, List.empty) | ||
|
||
if (digits.length < fractionDigits) List.fill(fractionDigits - digits.length)(0) ::: digits | ||
else digits | ||
} | ||
|
||
/** @return | ||
* half even rounded (preciseAmount, fractionDigits) to the cent value of the given currency | ||
*/ | ||
def roundHalfEven(preciseAmount: Long, fractionDigits: Int, currency: Currency): Long = { | ||
val centFractionDigits = fractionDigits - currency.getDefaultFractionDigits | ||
val power = pow10(centFractionDigits) | ||
val integer = preciseAmount / power | ||
val fraction = preciseAmount % power | ||
|
||
// Eg: 3 for 123.456 | ||
val leastSignificantDigitOfInt = integer % 10L | ||
|
||
val fractionDigitsList = getFractionDigits(fraction, centFractionDigits) | ||
|
||
// Eg: 4 for 123.456 | ||
val mostSignificantDigitOfFraction :: rest = fractionDigitsList | ||
|
||
if (mostSignificantDigitOfFraction == 5 && rest.forall(_ == 0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure where this number 5 is coming from. Can you explain? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the half even rounding we are interested in if we have exactly (Maybe firstDigitAfterDecimalPoint would be a better name for mostSignificantDigitOfFraction) 🤔 |
||
if (leastSignificantDigitOfInt % 2L == 0L) integer else integer + 1L | ||
else if (mostSignificantDigitOfFraction >= 5) | ||
integer + 1L | ||
else if (mostSignificantDigitOfFraction == -5 && rest.forall(_ == 0)) | ||
if (leastSignificantDigitOfInt % 2L == 0L) integer else integer - 1L | ||
else if (mostSignificantDigitOfFraction <= -5) | ||
integer - 1L | ||
else | ||
integer | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.sphere.util | ||
|
||
import org.scalatest.funspec.AnyFunSpec | ||
import org.scalatest.matchers.must.Matchers | ||
import org.scalatest.prop.TableDrivenPropertyChecks | ||
import org.scalatest.prop.TableDrivenPropertyChecks.Table | ||
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks | ||
|
||
import java.util.Currency | ||
import scala.math.BigDecimal.RoundingMode | ||
|
||
class MoneyRoundingSpec extends AnyFunSpec with Matchers with ScalaCheckDrivenPropertyChecks { | ||
val Euro: Currency = Currency.getInstance("EUR") | ||
val ZWL: Currency = Currency.getInstance("ZWL") | ||
val JPY: Currency = Currency.getInstance("JPY") | ||
|
||
describe("Money Rounding") { | ||
it("roundFloor should behave similarly to BigDecimal rounding") { | ||
ScalaCheckDrivenPropertyChecks.forAll(DomainObjectsGen.highPrecisionMoney) { h => | ||
val bdRes = HighPrecisionMoney.roundToCents(h.amount, h.currency)(RoundingMode.FLOOR) | ||
val longRes = | ||
MoneyRounding.roundFloor(h.preciseAmount, h.fractionDigits, h.currency) | ||
bdRes must be(longRes) | ||
} | ||
} | ||
|
||
it("roundCeiling should behave similarly to BigDecimal rounding") { | ||
ScalaCheckDrivenPropertyChecks.forAll(DomainObjectsGen.highPrecisionMoney) { h => | ||
benko-balog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val bdRes = HighPrecisionMoney.roundToCents(h.amount, h.currency)(RoundingMode.CEILING) | ||
val longRes = | ||
MoneyRounding.roundCeiling(h.preciseAmount, h.fractionDigits, h.currency) | ||
bdRes must be(longRes) | ||
} | ||
} | ||
|
||
it("roundHalfEven should behave similarly to BigDecimal rounding") { | ||
benko-balog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// I used random generated values later, but I needed these very specific values too to check the | ||
// edge cases of the half even rounding | ||
val data = Table( | ||
("preciseAmount", "fraction", "currency"), | ||
(1119L, 3, Euro), | ||
(1111L, 3, Euro), | ||
(1115L, 3, Euro), | ||
(1125L, 3, Euro), | ||
(112500L, 5, Euro), | ||
(11250001L, 7, Euro), | ||
(11000004L, 7, Euro), | ||
(11249999L, 7, Euro), | ||
(-1119L, 3, Euro), | ||
(-1111L, 3, Euro), | ||
(-1115L, 3, Euro), | ||
(-1125L, 3, Euro), | ||
(-112500L, 5, Euro), | ||
(5721482481806080960L, 6, ZWL), | ||
(123L, 0, JPY) | ||
) | ||
|
||
TableDrivenPropertyChecks.forAll(data) { (preciseAmount, fd, cur) => | ||
val amount = HighPrecisionMoney.preciseAmountToAmount(preciseAmount, fd) | ||
val bdRes = HighPrecisionMoney.roundToCents(amount, cur)(RoundingMode.HALF_EVEN) | ||
|
||
val longRes = | ||
MoneyRounding.roundHalfEven(preciseAmount, fd, cur) | ||
|
||
bdRes must be(longRes) | ||
} | ||
|
||
ScalaCheckDrivenPropertyChecks.forAll(DomainObjectsGen.highPrecisionMoney) { h => | ||
val bdRes = HighPrecisionMoney.roundToCents(h.amount, h.currency)(RoundingMode.HALF_EVEN) | ||
|
||
val longRes = | ||
MoneyRounding.roundHalfEven(h.preciseAmount, h.fractionDigits, h.currency) | ||
|
||
bdRes must be(longRes) | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.