File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed
scala-core-modules/scala-core-numbers-2/src
main/scala/com/baeldung/scala/roundingdecimals
test/scala/com/baeldung/scala/roundingdecimals Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -127,6 +127,14 @@ lazy val scala_core_numbers =
127
127
libraryDependencies += " org.scalatestplus" %% " scalacheck-1-17" % " 3.2.18.0" % Test
128
128
)
129
129
130
+ lazy val scala_core_numbers_2 =
131
+ (project in file(" scala-core-modules/scala-core-numbers-2" ))
132
+ .settings(
133
+ name := " scala-core-numbers" ,
134
+ libraryDependencies ++= scalaTestDeps,
135
+ scalaVersion := scala3Version
136
+ )
137
+
130
138
lazy val scala_core_io = (project in file(" scala-core-modules/scala-core-io" ))
131
139
.settings(
132
140
name := " scala-core-io" ,
Original file line number Diff line number Diff line change
1
+ package com .baeldung .scala .roundingdecimals
2
+
3
+ object RoundingDecimals {
4
+ def roundUp (decimal : Double ): Int =
5
+ math.ceil(decimal).toInt
6
+
7
+ def roundDown (decimal : Double ): Int =
8
+ math.floor(decimal).toInt
9
+
10
+ def roundToNearestWhole (decimal : Double ): Int =
11
+ math.round(decimal).toInt
12
+ }
Original file line number Diff line number Diff line change
1
+ package com .baeldung .scala .roundingdecimals
2
+
3
+ import com .baeldung .scala .roundingdecimals .RoundingDecimals
4
+ import org .scalatest .matchers .should .Matchers
5
+ import org .scalatest .wordspec .AnyWordSpec
6
+
7
+ class RoundingDecimalsUnitTest extends AnyWordSpec with Matchers {
8
+
9
+ " RoundingDecimals" should {
10
+ " Always round literals down" in {
11
+ 3 / 4 shouldBe 0
12
+ }
13
+ " Always round up in roundUp" in {
14
+ RoundingDecimals .roundUp(1.1 ) shouldBe 2
15
+ RoundingDecimals .roundUp(1.5 ) shouldBe 2
16
+ RoundingDecimals .roundUp(1.9 ) shouldBe 2
17
+ }
18
+ " Always round down in roundDown" in {
19
+ RoundingDecimals .roundDown(1.1 ) shouldBe 1
20
+ RoundingDecimals .roundDown(1.5 ) shouldBe 1
21
+ RoundingDecimals .roundDown(1.9 ) shouldBe 1
22
+ }
23
+ " Always round to nearest in roundDown" in {
24
+ RoundingDecimals .roundToNearestWhole(1.1 ) shouldBe 1
25
+ RoundingDecimals .roundToNearestWhole(1.5 ) shouldBe 2
26
+ RoundingDecimals .roundToNearestWhole(1.9 ) shouldBe 2
27
+ }
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments