Search code examples
rubymathfixnum

Ruby - cant divide with fixnum values


I need to calculate some % values by dividing two separate counts. I got the counts value which is class is fixnum. But while i am going to divide them, it showing me 0 whatever the count value is ,

<% dueTask = 12 %>
<% aT = 16 %>
<%= (dueTask/aT)*.to_f %>

Here Last result showing 0.


Solution

  • You can use two different lines in order to get the actual value.

        <% dueTask = 12 %>
        <% aT = 16 %>
        <% result = dueTask/aT.to_f %>
        <%= result %>
    

    In IRB you can get the value by checking this:

    dueTask = 12
    aT = 16
    dueTask/aT.to_f
    

    but this technique might not work when you have to show the result in HTML. the variables are of Fixnum type (you can check it by the following command in IRB shell)

    dueTask.class
    

    Output will be

    #=> Fixnum