Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upString.toFloat doesn't show decimal if nuls #964
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
mostlywhole
Jul 7, 2018
I wouldn't consider it a bug. On its own, the lack of a decimal represents absolute precision or type ambiguity. Thankfully, elm data is always accompanied by a type so we don't have to worry about the second possibility. See what happens when we declare a float in elm-repl.
> a = 123.0
123 : FloatWhich means when you try something like this:
a : Float
a = 123
b : Int
b = 123
a == b You find yourself looking at this error message
The right side of (==) is causing a type mismatch.
(==) is expecting the right side to be a:
Float
But the right side is:
Int
Hint: Elm does not automatically convert between Ints and Floats. Use `toFloat`
and `round` to do specific conversions.
<http://package.elm-lang.org/packages/elm-lang/core/latest/Basics#toFloat>
Hint: With operators like (==) I always check the left side first. If it seems
fine, I assume it is correct and check the right side. So the problem may be in
how the left and right arguments interact.
I agree that appending the decimal to the string conversion could be neat.
a = 123.0 |> toString
"123" : StringBut at that point, you could be applying string formatting, which would also give you your desired 0's.
mostlywhole
commented
Jul 7, 2018
|
I wouldn't consider it a bug. On its own, the lack of a decimal represents absolute precision or type ambiguity. Thankfully, elm data is always accompanied by a type so we don't have to worry about the second possibility. See what happens when we declare a float in elm-repl. > a = 123.0
123 : FloatWhich means when you try something like this: a : Float
a = 123
b : Int
b = 123
a == b You find yourself looking at this error message
I agree that appending the decimal to the string conversion could be neat. a = 123.0 |> toString
"123" : StringBut at that point, you could be applying string formatting, which would also give you your desired 0's. |
nitrajka commentedJun 7, 2018
•
edited
Edited 3 times
-
nitrajka
edited Jun 7, 2018 (most recent)
-
nitrajka
edited Jun 7, 2018
-
nitrajka
edited Jun 7, 2018
-
nitrajka
created Jun 7, 2018
Hello there,
while converting a
StringtoFloatI encountered a thing, that shouldn't happen in my opinion. When converting string number without a decimal number or with only zeros as decimal numbers to float, the converted number doesn't need to have at least one decimal. Consider these examples:String.toFloat "123" == Ok 123 --TrueString.toFloat "123.0" == Ok 123 --TrueString.toFloat "123" == Ok 123.0 --TrueString.toFloat "123.0" == Ok 123.0 --TrueThe former two seem like an Int to me, not a Float. Shouldn't they have at least one decimal number?
123.0? And because the latter two can be also the result of the same conversion, it seems to be inconsistent.Try this snippet and open the console. https://ellie-app.com/rM4LtHPchba1
I attach converting from string to float in other languages.
Converting from string to float works this way in Python:

Converting from string to float works this way in Java:
Is this a bug or a feature because Elm is compiled to JS? How is this possible?
Thank you :)