Reputation: 37658
I have two Int
values in Scala.
scala> val a = 3
a: Int = 3
scala> val b = 5
b: Int = 5
Now, I want to divide them and get Float. With as little boilerplate as possible.
If I do a/b
, I get
scala> a/b
res0: Int = 0
I cannot do simple Java (float)
.
scala> ((Float)a)/b
<console>:9: error: value a is not a member of object Float
((Float)a)/b
^
What should I do?
Upvotes: 23
Views: 43081
Reputation: 3696
Alternative answer that uses type ascription:
scala> (a:Float)/b
res0: Float = 0.6
Upvotes: 18
Reputation: 37658
The following line followed by its result should solve your problem.
scala> a.toFloat/b
res3: Float = 0.6
Upvotes: 36