myrho/elm-round/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

Round

This library empowers you to convert a Float to a String with ultimate control how many digits after the decimal point are shown and how the remaining digits are rounded. You can round, round up and round down the mathematical or the commerical way. For example:

x = 3.141592653589793


round 2 x -- "3.14"
round 4 x -- "3.1416"


ceiling 2 x -- "3.15"
floor 4 x -- "3.1415"

Commercial rounding means that negative and positive numbers are treated symmetrically. It affects numbers whose last digit equals 5. For example:

x = -0.5


round 0 x -- "0"
roundCom 0 x -- "-1"


floor 0 x -- "-1"
floorCom 0 x -- "0"


ceiling 0 x -- "0"
ceilingCom 0 x -- "-1"

Have a look at the tests for more examples!

Under the hood the Float is

* converted to a string * normalized (if it is in scientific notation, eg 1.234e-23) * splitted at the comma. * Then the part after comma is truncated to the number of desired digits + 1 * inserting a comma before the last digit, * turning this into a Float again, * apply a rounding function on it, * attach it the part before comma again. * By the way handles cases with already rounded numbers, zero and the sign.

Why aren't we just doing x * 1000 |> round |> toFloat |> (flip (/)) 1000 in order to round to 3 digits after comma? Because due to floating point arithmetic it might happen that it outputs someting like 3.1416000000001, although we just wanted 3.1416. Ugly.

Round to String

round : Int -> Float -> String

Turns a Float into a String and rounds it to the given number of digits after decimal point.

x = 3.141592653589793


round 2 x -- "3.14"
round 4 x -- "3.1416"
ceiling : Int -> Float -> String

Turns a Float into a String and rounds it up to the given number of digits after decimal point.

x = 3.141592653589793


ceiling 2 x -- "3.15"
ceiling 4 x -- "3.1416"
floor : Int -> Float -> String

Turns a Float into a String and rounds it down to the given number of digits after decimal point.

x = 3.141592653589793


floor 2 x -- "3.14"
floor 4 x -- "3.1415"
roundCom : Int -> Float -> String

Turns a Float into a String and rounds it to the given number of digits after decimal point the commercial way.

x = -0.5


round 0 x -- "0"
roundCom 0 x -- "-1"
ceilingCom : Int -> Float -> String

Turns a Float into a String and rounds it up to the given number of digits after decimal point the commercial way.

x = -0.5


ceiling 0 x -- "0"
ceilingCom 0 x -- "-1"
floorCom : Int -> Float -> String

Turns a Float into a String and rounds it down to the given number of digits after decimal point the commercial way.

x = -0.5


floor 0 x -- "-1"
floorCom 0 x -- "0"

Round to Float

roundNum : Int -> Float -> Float

As round but turns the resulting String back to a Float.

ceilingNum : Int -> Float -> Float

As ceiling but turns the resulting String back to a Float.

floorNum : Int -> Float -> Float

As floor but turns the resulting String back to a Float.

roundNumCom : Int -> Float -> Float

As roundCom but turns the resulting String back to a Float.

ceilingNumCom : Int -> Float -> Float

As ceilingCom but turns the resulting String back to a Float.

floorNumCom : Int -> Float -> Float

As floorCom but turns the resulting String back to a Float.

Utility functions

toDecimal : Float -> String

Transforms a Float in scientific notation into its decimal representation as a String.

x = 1e30
toDecimal x -- outputs "1000000000000000000000000000000"


x = 1.2345e-30
toDecimal x -- outputs "0.0000000000000000000000000000012345"
truncate : Float -> Int

Like Elm's basic truncate but works on the full length of a float's 64 bits. So it's more precise.

x = 9007199254740.99


Basics.truncate x -- 652835028 (which is not correct)
Round.truncate x -- 9007199254740 (which is)