999.0 Is this the expected behavior? How can I convert "9.99" to 9.99 +4 ruby Nathan Nov 12 '09 a...">

Ruby string to_f ... error?

"9.99".to_f => 999.0 

Is this the expected behavior? How can I convert "9.99" to 9.99

+4
source share
5 answers

What language do you work in? I guess he heals. "" as a separator of thousands (which in this case is in the wrong place) and "," as a decimal point.

Try

 "9,99".to_f 

... but if it works, it probably depends on the current culture of the system, and you should look to find a culturally invariant way of transformation.

+3
source

What version of Ruby are you using?

"9.99" .to_f leads to 9.99 for me using Ruby 1.8.6 on Windows.

+2
source

Which version? This works as expected on 1.8.7.

 irb(main):001:0> "9.99".to_f => 9.99 

.

 ruby -e "puts \"9.99\".to_f" 9.99 
+1
source

Perhaps some nasty pearl or Rails plugin has changed the behavior of your # to_f string ...

+1
source

Sure. This is a culture dependent conversion. Tested on IronRuby 0.9

-one
source

All Articles