Numbers

You’ve already seen the type signature number. A number value will work with all the familiar arithmetic operators.

> 7 + 2
9 : number
> 7 - 2
5 : number
> 7 * 2
14 : number
> 7 / 2
3.5 : Float

Two types of numbers: Float and Int

You may have noticed that this last operation yielded a value with a different type signature. That's because / is the floating-point division operator.

There is also a // integer division operator, which does division without the remainder. Its close friend is the % modulus operator, which gives the remainder left over from integer division.

> 7 // 2
3 : Int
> 7 % 2
1 : Int

Float and Int are specific types of numbers in Elm. We use Floats when we are interested in what comes after the decimal point. If you're only dealing with whole numbers, you're better off with an Int. This distinction is important for computers.

Type variables

Luckily for us humans, Elm doesn't force us to worry about this all the time. That's what that number type variable is about. When a value has the type signature number, Elm knows it's dealing with either a Float or an Int. However, it will wait to see what you end up doing with it before settling on either one.

results matching ""

    No results matching ""