How to Convert Float into Hours Minutes Seconds

Convert float into time (hour.minute) in python

Two cases:

a = "08.94"
asplit = a.split('.')

If the fraction is considered as minute:

print str(int(asplit[1])/60+int(asplit[0])) + '.' + str(int(asplit[1]) % 60)

If the fraction is considered as percentage of minutes:

print asplit[0] + '.' + str(int(float(asplit[1]) *  60))

Output:

9.34
08.5640

I need float hours from given hours,minutes,seconds

For converting minutes and seconds into hours you have to divide them by 60 and 3600 respectively.

float_hours=hours+minutes/60+seconds/3600

def to_float_hours(hours, minutes, seconds):
""" (int, int, int) -> float

Return the total number of hours in the specified number
of hours, minutes, and seconds.

Precondition: 0 <= minutes < 60 and 0 <= seconds < 60"""

return hours+minutes/60+seconds/3600

How to convert a float into time?

Sounds like you should convert it into a TimeSpan and then format that:

using System;

class Test
{
static void Main()
{
float totalSeconds = 228.10803f;
TimeSpan time = TimeSpan.FromSeconds(totalSeconds);
Console.WriteLine(time.ToString("hh':'mm':'ss")); // 00:03:48
}
}

An alternative would be to use my Noda Time library, where you'd construct a Duration and then format that.

Convert float to HH:MM format

You just need sprintf for the 0 padding, fmod to extract the fraction, and optionally round if flooring the seconds is unacceptable:

$time = 15.33;
echo sprintf('%02d:%02d', (int) $time, fmod($time, 1) * 60);

Convert float of seconds into float of minutes and seconds

If you want say 61 seconds to equal 1.01. Then you need division and modulo, then simply divide the remainder by 100 then add the results together.

Note : This is fairly suspect and can cause all sorts of problems with calculations in the future

var minutes = val / 60;
var seconds = (val % 60) / 100m;
Console.WriteLine(minutes+seconds);

How can you switch a float to a time format?

As with any problem, you need to break it down into its component parts and attack each one separately.

What you have:

  • A floating-point number of hours.

What you need:

  • To print the integer number of hours.
  • To print a ":"
  • To print the fractional number of hours as a proportion of 60 minutes.

So, now, we can address each part of the problem.

Let's say the input is:

const float time = 13.25;

The first part is quite easy — truncating a floating-point variable can be done using the mathematical floor function, but all you really need to do is cast to int to get the same effect:

std::cout << (unsigned int)time;

The second part is also really easy:

std::cout << ':';

The third part takes a little more work. We need to discard everything but the fractional part. We can do that by subtracting the integer part:

time - (unsigned int)time

Then we must transform the value so that instead of being a proportion of the range [0.00,1.00), it's a proportion of the range [0,60), simply by multiplying by 60:

60 * (time - (unsigned int)time)

We're left with:

const float time = 13.25;
std::cout << (unsigned int)time;
std::cout << ':';
std::cout << 60 * (time - (unsigned int)time);
// result: 13:15

(live demo)


For a general solution, we also want to show a leading zero if there's only one digit:

const float time = 12.10;
std::cout << (unsigned int)time;
std::cout << ':';
std::cout << std::setw(2) << std::setfill('0');
std::cout << 60 * ((time - (unsigned int)time) / 100);
// result: 12:06

In reality, to avoid rounding errors and possible overflows, you'd drop the float altogether and simply store integer minutes:

const unsigned int time_mins = (12*60) + 6;
std::cout << (time_mins / 60);
std::cout << ':';
std::cout << std::setw(2) << std::setfill('0');
std::cout << (time_mins % 60);
// result: 12:06

(live demo)

Or, y'know, use an actual time/date type.

Convert duration format from float to Month:Days:Hours:Minutes:Seconds in python 3.x

Another solution, if you want additional information like weeks:

import datetime

def time_in_sec(time_sec):

delta = datetime.timedelta(seconds=time_sec)
delta_str = str(delta)[-8:]
hours, minutes, seconds = [int(val) for val in delta_str.split(":", 3)]
weeks = delta.days // 7
days = delta.days % 7
return "{}:{}:{}:{}:{}".format(weeks, days, hours, minutes, seconds)


def time_in_sec2(seconds):

WEEK = 60 * 60 * 24 * 7
DAY = 60 * 60 * 24
HOUR = 60 * 60
MINUTE = 60

weeks = seconds // WEEK
seconds = seconds % WEEK
days = seconds // DAY
seconds = seconds % DAY
hours = seconds // HOUR
seconds = seconds % HOUR
minutes = seconds // MINUTE
seconds = seconds % MINUTE
return "{}:{}:{}:{}:{}".format(weeks, days, hours, minutes, seconds)


def main():
print(time_in_sec(12634.0))
print(time_in_sec2(12634.0))

The result is:

0:0:3:30:34
0.0:0.0:3.0:30.0:34.0

If you dont want information like weeks, so you can simply remove it

You can take solution 2 (time_in_sec2) if you dont want dependencies like

import datetime



Related Topics



Leave a reply



Submit