Scala math FAQ: How do I *square* a number in Scala, such as squaring an `Int`

, `Double`

, `Long`

, or `Float`

?

## Solution

You can square a number in Scala in at least 2-3 different ways:

- Multiply the number by itself
- Call the Java
`Math.pow`

function or the`scala.math.pow`

function

The following examples demonstrate these solutions.

NOTE: If you want to square a number in Java, you can also use the first two solutions I use below.

## 1) Scala: Square a number by multiplying it by itself

This is how you square a number in Scala by multiplying it by itself:

val i = 2 val square = i * i

In this example if you print the value of `square`

, it will be `4`

. An important point to know about this solution is that if you are squaring an integer in Scala, such as the number `2`

, the result will also be an `Int`

, in this case the value `4`

. You can use the same technique with `Int`

, `Double`

, `Long`

, and `Float`

.

## 2) Scala: Square a number with the Math.pow method

Here’s how you call the `Math.pow`

or `scala.math.pow`

methods to square a number:

// Math.pow val i = 2 val square = Math.pow(i, 2) // '2' means "square" // scala.math.pow val i = 2 val square = scala.math.pow(i, 2)

An important point to know about this solution is that if you square an integer, the result will actually be a `Double`

, so you’ll need to convert it back to an `Int`

or a `Long`

, if you want those types.

### More details

The Scala version of `pow`

is a function in the *scala.math* package object, as you can see in that Scaladoc. If you dig into the `scala.math.pow`

source code, you’ll see that it’s defined like this:

def pow(x: Double, y: Double): Double = java.lang.Math.pow(x, y)

Therefore, if you prefer, when squaring numbers you can call `Math.pow`

— which is `java.lang.Math.pow`

, because `java.lang.*`

is imported by default — directly, if you’d like to save a layer of abstraction.

Note that because the

`pow`

function takes a`Double`

as its input parameter, you can pass in a`Double`

,`Int`

,`Float`

, or`Long`

.

## Bonus: More Scala power multipliers

In general I just multiply the number by itself to get the *squared* value, but the advantage of the `Math.pow`

method is that once you know how to use it, you can *cube* a number and use other powers of multiplication like this:

val i = 2 val j = Math.pow(i, 2) // square (to the power of 2) val k = Math.pow(i, 3) // to the power of 3 val l = Math.pow(i, 4) // to the power of 4

As mentioned earlier, the disadvantage of this approach is that if you want to work with integers, the result of the `pow`

functions is a `Double`

, so if you want an `Int`

or `Long`

as the end result, you’ll have to convert that `Double`

back to an `Int`

or `Long`

.

## Bonus: How to convert from a Double to an Int (or Long)

Note that if you want to convert a `Double`

to an `Int`

, the correct solution is to test the `Double`

value to make sure it can be properly converted to an `Int`

. Do this with the `isValidInt`

function, as shown in these Scala REPL examples:

scala>9.9.isValidIntval res0: Boolean = false scala>9.0.isValidIntval res1: Boolean = true

As shown in those examples, the `Double`

value `9.9`

*cannot* be converted to an `Int`

, but `9.0`

*can* be converted to an `Int`

.

In a slightly-related note, functions like `ceil`

(ceiling), `floor`

, `round`

, `isWhole`

, and `longValue`

can also help when you want to convert a `Double`

value to an `Int`

or `Long`

:

scala>9.9.ceilval res2: Double = 10.0 scala>9.9.floorval res3: Double = 9.0 scala>9.9.roundval res4: Long = 10 scala>9.9.isWholeval res5: Boolean = false scala>9.9.longValueval res6: Long = 9

this post is sponsored by my books: |
|||

#1 New Release |
FP Best Seller |
Learn Scala 3 |
Learn FP Fast |

## Summary: How to square a number in Scala

In summary, if you wanted to see a few ways to square a number (`Int`

, `Double`

, `Float`

, or `Long`

) in Scala, I hope these examples are helpful.