Skip to content

Commit 6d8e53d

Browse files
cawpatCallum Gibbons
andauthored
[SCALA-490] Rounding Decimals (#1537)
* [SCALA-490] Rounding Decimals * [SCALA-490] Optimized imports * [SCALA-490] Fixed package names --------- Co-authored-by: Callum Gibbons <[email protected]>
1 parent 100661c commit 6d8e53d

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

build.sbt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ lazy val scala_core_numbers =
127127
libraryDependencies += "org.scalatestplus" %% "scalacheck-1-17" % "3.2.18.0" % Test
128128
)
129129

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+
130138
lazy val scala_core_io = (project in file("scala-core-modules/scala-core-io"))
131139
.settings(
132140
name := "scala-core-io",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)