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

  • Home
  • SEARCH
  • 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 7866419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:23:41+00:00 2026-06-03T00:23:41+00:00

I have the following method to convert a duration in seconds to hours. For

  • 0

I have the following method to convert a duration in seconds to hours. For example, 2 hours and a half would result in 2.30 Also, 2 hours and fifteen minutes would result in 2.15. So it outputs number of hours and number of minutes.

I want to modify the method to display 2.5 or 2.25 instead of the above. I need to do it that way to make calculations. For example, if there is a $30/hr salary and employee worked 10 hours and a half, I need to multiply 30*10.5 and not 30*10.30.

def hour_quantity
  unless self.duration.blank?
    hours = (self.duration/60)/60
    minutes = (self.duration/60) % 60
    hours.to_s + '.' + minutes.to_s
  end
end
  • 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-03T00:23:43+00:00Added an answer on June 3, 2026 at 12:23 am

    Jon is correct, as usual. Just use floating point division:

    def hour_quantity
      duration / 3600.0
    end
    

    When displaying, you might want to round:

    puts '%.2f' % hour_quantity
    

    This will give you 2 decimal places, always (e.g. “2.00”, “2.25”, “2.50”). Alternatively:

      puts '%g' % hour_quantity
      puts '%g' % 2.0           #=> "2"
      puts '%g' % 2.25          #=> "2.25"
      puts '%g' % 2.4999999     #=> "2.5"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method to convert String to date with millisecond granularity public
I have the following method to convert a String to Date object public Date
I have following XML configration i would like to convert to java property file.
I have following method which convert my custom DMY (date,month,year) object to Date. public
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method: private JobCard PopulateObject(JobCard jc, DataRow dataRow) { PropertyInfo[] proplist
I have the following method that is replacing a pound sign from the file
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have the following method that is supposed to be a generic Save to

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.