diff --git a/CHANGELOG.md b/CHANGELOG.md index 475ffd3e..61c7b8fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,13 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased/Snapshot] ### Added -- Added implicit classes for `loactiontec.jts` Geometries that represent geographical geometries with functionality before located in `GeoUtils` [#163] (https://github.com/ie3-institute/PowerSystemUtils/issues/163) +- Added implicit classes for `loactiontec.jts` Geometries that represent geographical geometries with functionality before located in `GeoUtils` [#163](https://github.com/ie3-institute/PowerSystemUtils/issues/163) - `OsmEntity` and `OsmContainer` to provide a simple, lightweight representation of openstreetmap data -- QuantityUtils previously implemented in SIMONA [#288] (https://github.com/ie3-institute/PowerSystemUtils/issues/288) +- QuantityUtils previously implemented in SIMONA [#288](https://github.com/ie3-institute/PowerSystemUtils/issues/288) ### Changed -- Refactored `GeoUtils`, moved them to the scala package and tailored them toward the `loactiontec.jts` Geometries used in the `OsmContainer` [#163] (https://github.com/ie3-institute/PowerSystemUtils/issues/163) -- Changed unit symbols according to DIN 1301-1 for apparent and reactive power [#278] (https://github.com/ie3-institute/PowerSystemUtils/issues/278) +- Refactored `GeoUtils`, moved them to the scala package and tailored them toward the `loactiontec.jts` Geometries used in the `OsmContainer` [#163](https://github.com/ie3-institute/PowerSystemUtils/issues/163) +- Changed unit symbols according to DIN 1301-1 for apparent and reactive power [#278](https://github.com/ie3-institute/PowerSystemUtils/issues/278) +- Rounding for quantities is now part of the `RichQuantity` [#314](https://github.com/ie3-institute/PowerSystemUtils/issues/314) ### Fixed - Fix tests in CI [#206](https://github.com/ie3-institute/PowerSystemUtils/issues/206) diff --git a/src/main/scala/edu/ie3/util/quantities/QuantityUtils.scala b/src/main/scala/edu/ie3/util/quantities/QuantityUtils.scala index 3050df47..3b0c7b9a 100644 --- a/src/main/scala/edu/ie3/util/quantities/QuantityUtils.scala +++ b/src/main/scala/edu/ie3/util/quantities/QuantityUtils.scala @@ -46,36 +46,6 @@ import scala.math.BigDecimal.RoundingMode.RoundingMode object QuantityUtils { - /** Rounds a quantity given a specified rounding mode after a specified - * decimal. - * - * @param quantity - * the quantity to round - * @param decimals - * how many decimals to consider - * @param roundingMode - * the rounding mode to use - * @tparam T - * type of the quantity - * @return - * the rounded quantity - */ - def round[T <: Quantity[T]]( - quantity: ComparableQuantity[T], - decimals: Int, - roundingMode: RoundingMode = RoundingMode.HALF_UP - ): ComparableQuantity[T] = { - if (decimals < 0) - throw new IllegalArgumentException( - "You can not round to negative decimal places." - ) - val rounded = BigDecimal - .valueOf(quantity.getValue.doubleValue()) - .setScale(decimals, roundingMode) - .doubleValue - Quantities.getQuantity(rounded, quantity.getUnit) - } - /** Implicit class to enrich the [[Double]] with [[ComparableQuantity]] * conversion capabilities * @@ -281,6 +251,33 @@ object QuantityUtils { def max(other: ComparableQuantity[Q]): ComparableQuantity[Q] = { if (q.isGreaterThan(other)) q else other } + + /** Rounds a quantity given a specified rounding mode after a specified + * decimal. + * + * @param decimals + * how many decimals to consider + * @param roundingMode + * the rounding mode to use + * @tparam Q + * type of the quantity + * @return + * the rounded quantity + */ + def round( + decimals: Int, + roundingMode: RoundingMode = RoundingMode.HALF_UP + ): ComparableQuantity[Q] = { + if (decimals < 0) + throw new IllegalArgumentException( + "You can not round to negative decimal places." + ) + val rounded = BigDecimal + .valueOf(q.getValue.doubleValue()) + .setScale(decimals, roundingMode) + .doubleValue + Quantities.getQuantity(rounded, q.getUnit) + } } implicit class RichUnit[Q <: Quantity[Q]]( diff --git a/src/test/scala/edu/ie3/util/quantities/QuantityUtilsSpec.scala b/src/test/scala/edu/ie3/util/quantities/QuantityUtilsSpec.scala index db7abfbd..8e608854 100644 --- a/src/test/scala/edu/ie3/util/quantities/QuantityUtilsSpec.scala +++ b/src/test/scala/edu/ie3/util/quantities/QuantityUtilsSpec.scala @@ -10,8 +10,7 @@ import edu.ie3.util.quantities.QuantityMatchers.equalWithTolerance import edu.ie3.util.quantities.QuantityUtils.{ RichQuantity, RichQuantityDouble, - RichUnit, - round + RichUnit } import org.scalatest.matchers.should.Matchers import org.scalatest.prop.TableDrivenPropertyChecks @@ -33,12 +32,12 @@ class QuantityUtilsSpec val qty = 10.1245.asAmpere "round a quantity half up by default" in { - round(qty, 2) should equalWithTolerance(10.12.asAmpere) - round(qty, 3) should equalWithTolerance(10.125.asAmpere) + qty.round(2) should equalWithTolerance(10.12.asAmpere) + qty.round(3) should equalWithTolerance(10.125.asAmpere) } "round a quantity with the given rounding mode" in { - round(qty, 3, RoundingMode.HALF_DOWN) should equalWithTolerance( + qty.round(3, RoundingMode.HALF_DOWN) should equalWithTolerance( 10.124.asAmpere ) }