This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

Toolkit.Float

Helpers for working with Float values

Rounding

roundTo : Int -> Float -> Float

Round a Float to a given number of decimal places

pi
  |> roundTo 2

--> 3.14

pi
  |> roundTo 0

--> 3

1234
  |> roundTo -2

--> 1200
module Toolkit.Float exposing
  ( roundTo )

{-|

Helpers for working with `Float` values

# Rounding
@docs roundTo

-}


{-| Round a `Float` to a given number of decimal places

    pi
      |> roundTo 2

    --> 3.14

    pi
      |> roundTo 0

    --> 3

    1234
      |> roundTo -2

    --> 1200

-}
roundTo : Int -> Float -> Float
roundTo place number =
  let
    multiplier =
      place
        |> (^) 10
        |> toFloat

  in
    number
      |> (*) multiplier
      |> round
      |> toFloat
      |> flip (/) multiplier