Converting mins into seconds?

This is a discussion on Converting mins into seconds? within the C Programming forums, part of the General Programming Boards category; Code: double baking=.5*(45)+45; double baking_seconds= In the above example, the top variable would give 67.5 minutes. How do i write ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    140

    Converting mins into seconds?

    Code:
     double baking=.5*(45)+45;
     
    double baking_seconds=

    In the above example, the top variable would give 67.5 minutes.

    How do i write a code such that it'll show 67 mins and 30 seconds?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Extract the integer and fractional parts.
    modf(3) - Linux man page

    Or do x - (int)x to get the fraction.

    Or do the whole thing as integer seconds, then use /60 and %60 to get minutes and seconds.

    Or.....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    Perhaps you can use two ints: seconds and minutes. Do all timing calculations in terms of seconds, and to convert to minutes and seconds, while seconds > 60, subtract 60 and increment minutes.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Quote Originally Posted by Salem View Post
    Extract the integer and fractional parts.
    modf(3) - Linux man page

    Or do x - (int)x to get the fraction.

    Or do the whole thing as integer seconds, then use /60 and %60 to get minutes and seconds.

    Or.....
    would modulo sign work in this case?
    67.5%60= .5?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    Read carefully:

    Or do the whole thing as integer seconds, then use /60 and %60 to get minutes and seconds.
    And if you have a compiler at your disposal, why not just try it and see what happens?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by tmac619619 View Post
    would modulo sign work in this case?
    67.5%60= .5?
    No, actually it would not because you cant use the modulus operator with floats.
    Even if it did work, that mathematical calculation would give you 7.5.
    You didn't think that through well.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    There would be a way to use modulo, though, right?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by gratiafide View Post
    There would be a way to use modulo, though, right?
    This is one way:

    double baking=67.5
    int temp = baking //get the int part of baking
    seconds = temp * 3600 //and convert them to seconds

    //handle fractional hours to seconds
    temp = (baking * 100)*0.6 //calculate seconds from fractional part of baking,

    and add them to the seconds from the int part
    seconds+=temp; //no fractional part doesn't cause an error

    print up seconds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert seconds to hours, minutes and seconds
    By kkk in forum C Programming
    Replies: 2
    Last Post: 07-26-2011, 11:47 AM
  2. Converting localtime seconds into float
    By superpickle in forum C Programming
    Replies: 2
    Last Post: 05-12-2007, 06:09 PM
  3. Converting seconds to hours in decimal form?
    By ajmcello in forum C Programming
    Replies: 3
    Last Post: 04-24-2006, 03:35 AM
  4. Dev C++ takes 3 mins to reload a project
    By phil in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-01-2005, 03:32 PM
  5. Converting seconds to years, months etc...
    By ^xor in forum C Programming
    Replies: 7
    Last Post: 08-07-2005, 12:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21