Baeldung Pro – Scala – NPI EA (cat = Baeldung on Scala)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

In this article, we’ll learn what to expect when using division operations in Scala. Then, we’ll explore a variety of functions available on Doubles in Scala, to allow us to round decimal values to whole numbers.

2. Dividing Ints

When using division on Ints in Scala, the operation will always return an Int. However, as we know, division doesn’t always result in a whole number. So, this can sometimes produce some unexpected results. Therefore, it’s important to understand what happens.

Let’s look at the following example:

val aNumber: Int = 3/4
// aNumber = 0

Here we have a val called aNumber, and we’ve assigned the resulting value of the calculation 3 divided by 4. Using basic arithmetic we know this calculation equals 0.75. However, since the type is Int, aNumber is unable to hold a decimal value.

In any case, where the division of two Ints returns a decimal value, the decimal places are always truncated, meaning the value is rounded down. So, the value of aNumber in our example above is zero.

2.1. Return a Double from Dividing Ints

We can fix this problem by converting either the dividend or divisor to a Double, using .toDouble. Which can be called directly on an Int literal:

val aNumber: Double = 3.toDouble/4
// aNumber = 0.75

Now we’ve converted part of the equation to a Double, the return type of aNumber is also Double. With a value of 0.75.

3. Rounding Up

If we do want to have control over the rounding of Doubles containing decimal places, there are a variety of functions available in Scala.math.

To start with, let’s look at how we round up to a whole number:

val aNumber: Int = math.ceil(0.75).toInt
// aNumber = 1

Using math.ceil we can round a Double up to a whole number. Since we know the result will always be a whole number, we can safely convert the result to an Int, using .toInt.

In this example, aNumber will be one, since math.ceil rounds everything up. We could have any decimal value between zero and one, and it would still round up to one.

4. Rounding Down

On the flip side, if we want to round down to a whole number we can use math.floor:

val aNumber: Int = math.floor(0.75).toInt
// aNumber = 0

As we can see, this works in the same way as math.ceil. However, the result will always be rounded down. Therefore, in this example, the result will be zero. The result same result will be produced for any value between zero and one.

5. Rounding to the Nearest Whole Number

Potentially, the most common rounding operation we’d need to do in our code would be to round a decimal to the nearest whole number. Where anything above .5 rounds up and anything below rounds down. This can be achieved using math.round:

val aNumber: Int = math.round(0.75).toInt
// aNumber = 1

Again, this works in the same way as our previous examples and here aNumber would be 1. If we had a value of 0.5, it would round up to one and a value of 0.4 would round down to zero.

6. Conclusion

In this article, we’ve seen what to expect when using division operations in our Scala code. We then explored how to control the rounding rules when dealing with decimal places on Doubles. We know math.ceil can be used for rounding up, math.floor for rounding down, and finally, math.round to round to the nearest whole number.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.