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 7958167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:22:45+00:00 2026-06-04T04:22:45+00:00

Possible Duplicate: Ruby Refuses to Divide Correctly I am using Ruby on Rails 3.2.2

  • 0

Possible Duplicate:
Ruby Refuses to Divide Correctly

I am using Ruby on Rails 3.2.2 and when I try to calculate 10 * (50 / 100) I got back 0 but when I make 10 * 50 / 100 I got back 5.

For technical reasons, I can not use the latter since my code is like the following:

perc = 50 /100

# Some code that uses the 'perc' variable
... = method_name(perc)

return 10 * perc

What should I make to solve the problem and have 5 (not 5.0 or something else; that is, so that the returns is an Integer)?

  • 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-04T04:22:46+00:00Added an answer on June 4, 2026 at 4:22 am

    50 / 100 is 0 because you are using integer division. The result is truncated to an integer.

    Performing the multiplication first works because then the division gives a whole number.

    You could perform the calculation using floats instead of integers: 50.0 / 100.0 == 0.5, then round the result to an integer at the end of the calculation.

    proportion = 50.0 / 100.0
    result = (10 * proportion).round
    return result
    

    See it working online: ideone

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Ruby/Ruby on Rails ampersand colon shortcut As a habit I try and
Possible Duplicate: Ruby: difference between || and ‘or’ Using Ruby || and or are
Possible Duplicate: Ruby 1.9.2 and Rails 3 cannot open rails console I have already
Possible Duplicate: Rails 3.1 and Ruby 1.9.3p125: ruby-debug19 still crashes with “Symbol not found:
Possible Duplicate: What does map(&:name) mean in Ruby? Ruby/Ruby on Rails ampersand colon shortcut
Possible Duplicate: Can't convert String into integer in ruby/ruby-on-rails I'm running through a tutorial
Possible Duplicate: Is it possible to run Ruby on Rails with Ruby 1.9x? Browsing
Possible Duplicate: Ruby on Rails scalability/performance? I'm aware from the twitter shortages that Ruby
Possible Duplicate: Perplexing Ruby/MySQL Error: "invalid packet: sequence number mismatch" My rails app just
Possible Duplicate: How do I make part of a regular expression optional in Ruby?

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.