Ruby Language Casting (type conversion) Casting to a Float

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

"123.50".to_f   #=> 123.5
Float("123.50") #=> 123.5

However, there is a difference when the string is not a valid Float:

"something".to_f   #=> 0.0
Float("something") # ArgumentError: invalid value for Float(): "something"


Got any Ruby Language Question?