Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String.toFloat doesn't show decimal if nuls #964

Open
nitrajka opened this issue Jun 7, 2018 · 1 comment
Open

String.toFloat doesn't show decimal if nuls #964

nitrajka opened this issue Jun 7, 2018 · 1 comment

Comments

@nitrajka
Copy link

nitrajka commented Jun 7, 2018

Hello there,

while converting a String to Float I 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 --True
String.toFloat "123.0" == Ok 123 --True
String.toFloat "123" == Ok 123.0 --True
String.toFloat "123.0" == Ok 123.0 --True

The 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:
screen shot 2018-06-07 at 14 42 57

Converting from string to float works this way in Java:

String f = "12";
System.out.println(Float.parseFloat(f)); //12.0

Is this a bug or a feature because Elm is compiled to JS? How is this possible?
Thank you :)

@mostlywhole
Copy link

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 : Float

Which 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" : String

But at that point, you could be applying string formatting, which would also give you your desired 0's.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants