 <p>You should be using <a href="http://ruby-doc.org/core/classes/Kernel.html#M005965" rel="nofollow noreferrer"><code>Kernel::Float</code></a> to convert the number; on invalid input, this will raise an error instead of just "trying" to convert it.</p> <pre><code>&gt;&gt; "10.5".to_f =&gt; 10.5 &gt;&gt; "asdf".to_f # do you *really* want a zero for this? =&gt; 0.0 &gt;&gt; Float("asdf") ArgumentError: invalid value for Float(): "asdf" from (irb):11:in `Float' from (irb):11 &gt;&gt; Float("10.5") =&gt; 10.5 </code></pre>