This library converts a Float
to a String
with ultimate
control how many digits after the decimal point are shown and how the remaining
digits are rounded. It rounds, floors and ceils the "common" way (ie. half
up) or the "commerical"
way (ie. half away from
zero).
Example:
x = 3.141592653589793
round 2 x -- "3.14"
round 4 x -- "3.1416"
ceiling 2 x -- "3.15"
floor 4 x -- "3.1415"
The given number of digits after decimal point can also be negative.
x = 213.14
round -2 x -- "200"
round -1 x -- "210"
ceiling -2 x -- "300"
floor -3 x -- "0"
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!
Why couldn't you just do x * 1000 |> round |> toFloat |> (flip (/)) 1000
in
order to round to 3 digits after comma? Due to floating point
arithmetic it might happen that it results into someting like
3.1416000000001
,
although we just wanted 3.1416
.
Under the hood this library converts the Float
into a String
and rounds it
char-wise. Hence it's safe from floating point arithmetic weirdness.
round : Basics.Int -> Basics.Float -> String
Turns a Float
into a String
and
rounds it to the given
number of digits
after decimal point. Behaves like Basics.round
.
x = 3.141592653589793
round 2 x -- "3.14"
round 4 x -- "3.1416"
The number of digits after decimal point can also be negative.
x = 213.35
round -2 x -- "200"
round -1 x -- "210"
ceiling : Basics.Int -> Basics.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"
The number of digits after decimal point can also be negative.
x = 213.35
ceiling -2 x -- "300"
ceiling -1 x -- "220"
floor : Basics.Int -> Basics.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"
The number of digits after decimal point can also be negative.
x = 213.35
floor -2 x -- "200"
floor -1 x -- "210"
roundCom : Basics.Int -> Basics.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"
The number of digits after decimal point can also be negative.
ceilingCom : Basics.Int -> Basics.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"
The number of digits after decimal point can also be negative.
floorCom : Basics.Int -> Basics.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"
The number of digits after decimal point can also be negative.
roundNum : Basics.Int -> Basics.Float -> Basics.Float
As round
but turns the resulting String
back to a Float
.
ceilingNum : Basics.Int -> Basics.Float -> Basics.Float
As ceiling
but turns the resulting String
back to a Float
.
floorNum : Basics.Int -> Basics.Float -> Basics.Float
As floor
but turns the resulting String
back to a Float
.
roundNumCom : Basics.Int -> Basics.Float -> Basics.Float
As roundCom
but turns the resulting String
back to a Float
.
ceilingNumCom : Basics.Int -> Basics.Float -> Basics.Float
As ceilingCom
but turns the resulting String
back to a Float
.
floorNumCom : Basics.Int -> Basics.Float -> Basics.Float
As floorCom
but turns the resulting String
back to a Float
.
toDecimal : Basics.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 : Basics.Float -> Basics.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)