如何从item.text获取真正的String并将其转换为Ruby上的float

时间:2022-01-13 21:43:25

I'm getting a number from item.text input. I want to convert that string to float. Now I'm trying with this:

我从item.text输入中得到一个数字。我想将该字符串转换为float。现在我正在尝试这个:

deci = @wert.text.to_s
para deci.to_f

The problem is, that I get something with '232.2' instead of "232.2". The .to_f function is only working with strings with the type of "..."

问题是,我得到的东西是“232.2”而不是“232.2”。 .to_f函数仅适用于类型为“...”的字符串

3 个解决方案

#1


0  

Try

尝试

@wert.text.first.to_s

instead of

代替

@wert.text.to_s

#2


0  

Have you tried something like this?

你尝试过这样的事吗?

Float("232.2")

#3


0  

In my irb(ruby 1.9.3p429), it is working fine:

在我的irb(ruby 1.9.3p429)中,它工作正常:

"232.2".to_f ==> 232.2            '232.2'.to_f ==> 232.2

Other options you have:

您有其他选择:

Float("232.2") ==> 232.2            Float('232.2') ==> 232.2
eval("232.2") ==> 232.2            eval('232.2') ==> 232.2

#1


0  

Try

尝试

@wert.text.first.to_s

instead of

代替

@wert.text.to_s

#2


0  

Have you tried something like this?

你尝试过这样的事吗?

Float("232.2")

#3


0  

In my irb(ruby 1.9.3p429), it is working fine:

在我的irb(ruby 1.9.3p429)中,它工作正常:

"232.2".to_f ==> 232.2            '232.2'.to_f ==> 232.2

Other options you have:

您有其他选择:

Float("232.2") ==> 232.2            Float('232.2') ==> 232.2
eval("232.2") ==> 232.2            eval('232.2') ==> 232.2