Error: Kotlin: The floating-point literal does not conform to the expected type Float
Asked Answered
L

1

10

I was making a simple maths calculator in kotlin, an error appeared on my screen when I tried to initialize the value of one of the variables used as 0.00 for float integer.

var x:Float= readLine()!!.toFloat()
var y:Float= readLine()!!.toFloat()
var sum:Float=0.00// the error message is showcased in this line
sum=x+y
println("Addition " + sum)
Lifeline answered 23/6, 2019 at 17:56 Comment(9)
0.00 is considered as Double. Change to this: var sum:Float=0.00f. The f suffix means FloatCondyle
Or use 0.00.toFloat()Condyle
You can read about numeric literals here.Superdominant
Moved my comment to an answer so that I could add code and have it format correctly. Please refer to that. Hope it helps!Mossman
@Steve thanks a lot for the insights, but if I'll remove the float specification or rather any datatype specification then how would kotlin treat these variables, under what datatype?Lifeline
It uses the type of the right hand side of the expression...the type of the value that the variable is being assigned to. In your case, x and y will be Float values because you quite clearly are assigning Float values to them, as that's what toFloat() returns. Likewise, sum will be a Float because adding two Float values results in a Float value.Mossman
Thanks a lot, you made the concept quite clear @SteveLifeline
Please mark my answer as addressing your question by clicking the "checkmark" on my answer if it truly does. I wouldn't mind getting some points, but I ask more so that we all do what we can to make S.O. as useful to future readers as possible.Mossman
Fixed this way https://mcmap.net/q/359387/-0xff0000ff-an-integer-literal-does-not-conform-to-the-expected-type-kotlin-intPity
M
7

This is a key difference between Java and Kotlin. Kotlin does not do numeric type promotion like Java does. The comments to your question are showing you how to deal with this, by either matching up the two types Double and/or Float to begin with, or by explicitly converting one or the other so that the two types match up.

Your problems goes away if you make use of Kotlin's ability to infer variable types by taking the type specifications off of your variable definitions. The fact that Kotlin infers types is one reason it does not promote numeric types. Mixing the two would lead to a lot of confusion.

Here's an example of how to fix and simplify your code's type mismatch issues using type inference:

var x = readLine()!!.toFloat()
var y = readLine()!!.toFloat()
var sum = x + y
println("Addition " + sum)

I understand that this may be just test code that you're using to understand Kotlin better. With that said, I'll point out that this code will crash if your user types in non-numeric input. You could fix this by putting a try/catch around your input lines, and providing an nice error message. You might want to put each input in a loop, continuing to ask for an input until the user does provide a response that is of the expected format.

Mossman answered 23/6, 2019 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.