Morph for an arbitrary-sized Decimal
MorphRow from chars to a Decimal number.
import Morph
import List.Morph
-- trailing 0s are matched but have no effect
"12.0340000"
|> Morph.toNarrow
(Decimal.Morph.chars
|> Morph.rowFinish
|> Morph.over List.Morph.string
)
--→ Ok with a Decimal representing
--→ 12.034
"-12.000"
|> Morph.toNarrow
(Decimal.Morph.chars
|> Morph.rowFinish
|> Morph.over List.Morph.string
)
--→ Ok with a Decimal representing
--→ -12.0
-- leading floating point is allowed
".012"
|> Morph.toNarrow
(Decimal.Morph.chars
|> Morph.rowFinish
|> Morph.over List.Morph.string
)
--→ Ok with a Decimal representing
--→ 0.012
"-.12"
|> Morph.toNarrow
(Decimal.Morph.chars
|> Morph.rowFinish
|> Morph.over List.Morph.string
)
--→ Ok with a Decimal representing
--→ -0.12
-- fails for integers without a floating point, see the not below
"12"
|> Morph.toNarrow
(Decimal.Morph.chars
|> Morph.rowFinish
|> Morph.over List.Morph.string
)
|> Result.toMaybe
--> Nothing
-- but succeeds for integers with a trailing floating point
"12."
|> Morph.toNarrow
(Decimal.Morph.chars
|> Morph.rowFinish
|> Morph.over List.Morph.string
)
--→ Ok with a Decimal representing
--→ 12.0
-- exponential notation, other letters, symbols etc make it fail
"."
|> Morph.toNarrow
(Decimal.Morph.chars
|> Morph.rowFinish
|> Morph.over List.Morph.string
)
|> Result.toMaybe
--> Nothing
"3e10"
|> Morph.toNarrow
(Decimal.Morph.chars
|> Morph.rowFinish
|> Morph.over List.Morph.string
)
|> Result.toMaybe
--> NothingTo allow integers to parse as decimals as well,
build a Morph.choice between
Decimal.Morph.chars
and Integer.Morph.chars
The fact that "12." parses as 12 might also seem weird to you.
If you don't want to allow that,
you'll need to spin your own version, taking this implementation as a reference.
It's not that scary I swear!
MorphRow from from Bits to a Decimal
Float MorphValue
MorphValue from an Exception
FloatMorph
an elm/core Float
to a Result Exception Decimal
Keep in mind that DecimalOrException -> Float can be lossy
since Float is fixed in bit size while Decimal is not.
Inverse of Float.Morph.decimalOrException
If you need a narrow Decimal, not a result, try
Result.Morph.toOk
|> Morph.over Decimal.Morph.orExceptionFloat
|> Morph.errorMap (Morph.deadEndMap Decimal.exceptionToString)
MorphValuefrom aDecimalTo get a
MorphValuefrom aFloat, useFloat.Morph.decimalOrExceptionoverDecimal.Morph.orExceptionValue