Skip to content

Commit 1bfea68

Browse files
Merge pull request #315 from ie3-institute/to/#314-mv-qty-rounding
Move qty rounding
2 parents b32a12c + 57c0fbc commit 1bfea68

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased/Snapshot]
88

99
### Added
10-
- 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)
10+
- 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)
1111
- `OsmEntity` and `OsmContainer` to provide a simple, lightweight representation of openstreetmap data
12-
- QuantityUtils previously implemented in SIMONA [#288] (https://github.com/ie3-institute/PowerSystemUtils/issues/288)
12+
- QuantityUtils previously implemented in SIMONA [#288](https://github.com/ie3-institute/PowerSystemUtils/issues/288)
1313

1414
### Changed
15-
- 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)
16-
- Changed unit symbols according to DIN 1301-1 for apparent and reactive power [#278] (https://github.com/ie3-institute/PowerSystemUtils/issues/278)
15+
- 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)
16+
- Changed unit symbols according to DIN 1301-1 for apparent and reactive power [#278](https://github.com/ie3-institute/PowerSystemUtils/issues/278)
17+
- Rounding for quantities is now part of the `RichQuantity` [#314](https://github.com/ie3-institute/PowerSystemUtils/issues/314)
1718

1819
### Fixed
1920
- Fix tests in CI [#206](https://github.com/ie3-institute/PowerSystemUtils/issues/206)

src/main/scala/edu/ie3/util/quantities/QuantityUtils.scala

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,6 @@ import scala.math.BigDecimal.RoundingMode.RoundingMode
4646

4747
object QuantityUtils {
4848

49-
/** Rounds a quantity given a specified rounding mode after a specified
50-
* decimal.
51-
*
52-
* @param quantity
53-
* the quantity to round
54-
* @param decimals
55-
* how many decimals to consider
56-
* @param roundingMode
57-
* the rounding mode to use
58-
* @tparam T
59-
* type of the quantity
60-
* @return
61-
* the rounded quantity
62-
*/
63-
def round[T <: Quantity[T]](
64-
quantity: ComparableQuantity[T],
65-
decimals: Int,
66-
roundingMode: RoundingMode = RoundingMode.HALF_UP
67-
): ComparableQuantity[T] = {
68-
if (decimals < 0)
69-
throw new IllegalArgumentException(
70-
"You can not round to negative decimal places."
71-
)
72-
val rounded = BigDecimal
73-
.valueOf(quantity.getValue.doubleValue())
74-
.setScale(decimals, roundingMode)
75-
.doubleValue
76-
Quantities.getQuantity(rounded, quantity.getUnit)
77-
}
78-
7949
/** Implicit class to enrich the [[Double]] with [[ComparableQuantity]]
8050
* conversion capabilities
8151
*
@@ -281,6 +251,33 @@ object QuantityUtils {
281251
def max(other: ComparableQuantity[Q]): ComparableQuantity[Q] = {
282252
if (q.isGreaterThan(other)) q else other
283253
}
254+
255+
/** Rounds a quantity given a specified rounding mode after a specified
256+
* decimal.
257+
*
258+
* @param decimals
259+
* how many decimals to consider
260+
* @param roundingMode
261+
* the rounding mode to use
262+
* @tparam Q
263+
* type of the quantity
264+
* @return
265+
* the rounded quantity
266+
*/
267+
def round(
268+
decimals: Int,
269+
roundingMode: RoundingMode = RoundingMode.HALF_UP
270+
): ComparableQuantity[Q] = {
271+
if (decimals < 0)
272+
throw new IllegalArgumentException(
273+
"You can not round to negative decimal places."
274+
)
275+
val rounded = BigDecimal
276+
.valueOf(q.getValue.doubleValue())
277+
.setScale(decimals, roundingMode)
278+
.doubleValue
279+
Quantities.getQuantity(rounded, q.getUnit)
280+
}
284281
}
285282

286283
implicit class RichUnit[Q <: Quantity[Q]](

src/test/scala/edu/ie3/util/quantities/QuantityUtilsSpec.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import edu.ie3.util.quantities.QuantityMatchers.equalWithTolerance
1010
import edu.ie3.util.quantities.QuantityUtils.{
1111
RichQuantity,
1212
RichQuantityDouble,
13-
RichUnit,
14-
round
13+
RichUnit
1514
}
1615
import org.scalatest.matchers.should.Matchers
1716
import org.scalatest.prop.TableDrivenPropertyChecks
@@ -33,12 +32,12 @@ class QuantityUtilsSpec
3332
val qty = 10.1245.asAmpere
3433

3534
"round a quantity half up by default" in {
36-
round(qty, 2) should equalWithTolerance(10.12.asAmpere)
37-
round(qty, 3) should equalWithTolerance(10.125.asAmpere)
35+
qty.round(2) should equalWithTolerance(10.12.asAmpere)
36+
qty.round(3) should equalWithTolerance(10.125.asAmpere)
3837
}
3938

4039
"round a quantity with the given rounding mode" in {
41-
round(qty, 3, RoundingMode.HALF_DOWN) should equalWithTolerance(
40+
qty.round(3, RoundingMode.HALF_DOWN) should equalWithTolerance(
4241
10.124.asAmpere
4342
)
4443
}

0 commit comments

Comments
 (0)