tags:

views:

173

answers:

5
"9.99".to_f
=> 999.0

Is this the expected behavior? How would one convert "9.99" to 9.99

+3  A: 

What locale are you running in? My guess is that it's treating "." as a thousands separator (which happens to be in the wrong place in this case) and "," as a decimal point.

Try

"9,99".to_f

... but if that works, it's probably dependent on the current culture of the system, and you should look to find a culture-invariant way of converting.

Jon Skeet
`irb(main):002:0> "9,99".to_f``=> 9.0`I'm on windows, Ruby 1.8.6.
Chirantan
@Chirantan: The question is what it does on the OP's computer. That will help to detect whether it's a culture-related issue.
Jon Skeet
+2  A: 

What version of Ruby are you using ?

"9.99".to_f results in 9.99 for me using Ruby 1.8.6 on Windows.

Reuben Mallaby
+1  A: 

What 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
ezpz
A: 

Maybe some nasty gem or a Rails plugin changed your String#to_f behaviour...

Mladen Jablanović
A: 

Sure. It's a culture dependent conversion. Tested on IronRuby 0.9

Sergey Mirvoda