Casting to a Float

Download ruby eBook

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"

Stats

Contributors: 3
2016-10-24
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook