Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8811191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:18:06+00:00 2026-06-14T03:18:06+00:00

I can’t figure out why a calculation is wrong. I’m trying to calculate the

  • 0

I can’t figure out why a calculation is wrong. I’m trying to calculate the percent of time that the user scores over the median score in my Rails app.

controller

my_scores_over_median_count = 6
total_scores_over_median = 8
@my_over_median_percent = (my_scores_over_median_count / total_scores_over_median) * 100

When I add this to my controller:

puts my_scores_over_median_count
puts total_scores_over_median 
puts @my_over_median_percent

I get:

6 (correct)
8 (correct)
0 (incorrect)

Can someone please help me understand why I’m getting 0 instead of 75 ? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:18:08+00:00Added an answer on June 14, 2026 at 3:18 am

    classic integer division problem. Change it to this and see it work:

    my_scores_over_median_count = 6.0
    total_scores_over_median = 8.0
    @my_over_median_percent = (my_scores_over_median_count / total_scores_over_median) * 100
    

    Ruby interprets 6 as a ‘Fixnum’ class, essentially an integer. We know this by:

    1.9.3-p194 :001 > 6.class
     => Fixnum 
    

    Dividing any number by a Fixnum causes Ruby to do integer math which means any remainder gets thrown out. So:

    1.9.3-p194 :004 > 4 / 5
     => 0 
    1.9.3-p194 :005 > 4 / 3
     => 1 
    

    If you have a variable that is a Fixnum, and you want to convert it to a float to force floating point division, you use the .to_f method:

    1.9.3-p194 :007 > x = 4
     => 4 
    1.9.3-p194 :008 > y = 5
     => 5 
    1.9.3-p194 :009 > x / y
     => 0 
    1.9.3-p194 :010 > x.to_f / y
     => 0.8 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can anyone help me trying to find out why this doesn't work. The brushes
Can someone please help me figure out why IE9 won't load my google map
Can anyone please explain to me how to figure out the type of the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I call select before recv_from on a socket that is blocking?
Can I have a project that has some parts written in c and other
Can some one confirm me that only one UIWindow instance is possible in any
Can we change the color of the divider? Apple documentations says, that we can
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.