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.
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"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"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"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"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"As round but turns the resulting String back to a Float.
As ceiling but turns the resulting String back to a Float.
As floor but turns the resulting String back to a Float.
As roundCom but turns the resulting String back to a Float.
As ceilingCom but turns the resulting String back to a Float.
As floorCom but turns the resulting String back to a Float.
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"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)
Turns a
Floatinto aStringand rounds it to the given number of digits after decimal point.