Using floats in a rule

OK, here is another kind of basic question.

If I have a string that contains something like “22.45” how do I convert that to a float value?

Lets say I have:

val String foo = "22.45"
val float f1 = foo
val float f2 = f1 * 1.5

Then when the code executes I get the following exception:

Could not invoke method: org.eclipse.xtext.xbase.lib.FloatExtensions.operator_multiply(float,double) on instance: null

It looks like maybe the f1 variable is not really a float??

What am I doing wrong?

You might need to do something like:

val float f1 = Float::parseFloat(foo)

1 Like