Elm:
Rounding numbers

How to:

Elm’s Basics module provides nifty functions for rounding: round, floor, and ceiling. Here’s how to use them.

import Basics exposing (round, floor, ceiling)

-- Round to the nearest whole number
round 3.14    --> 3
round 3.5     --> 4

-- Round down
floor 3.999   --> 3

-- Round up
ceiling 3.001 --> 4

-- Truncate decimals without rounding
truncate 3.76 --> 3

Elm also provides toLocaleString for rounding to a fixed number of decimal places:

import Float exposing (toLocaleString)

-- Round to two decimal places
toLocaleString 2 3.14159 --> "3.14"

Deep Dive

Elm is a strongly typed functional language that relegates side effects to the architecture’s “edges.” This means that functions like rounding must be pure and predictable. Historically, rounding is a common operation in many programming languages that deal with the imprecision of floating-point arithmetic.

Elm’s approach to rounding is straightforward - the functions are pure and adhere to mathematical definitions for round, floor, and ceiling. Elm anticipates the common needs by providing built-in functions, as precision management is a frequent requirement, especially in finance and graphics.

Alternatives to Elm’s built-in functions could include custom implementations using arithmetic operations, but that adds unnecessary complexity when the standard library already does the job efficiently.

As of the current version, Elm uses JavaScript’s underlying floating-point math for these operations, hence staying consistent with the IEEE 754 standard, which is something to remember when considering precision and potential floating-point errors.

See Also