I have the following function:
function timestamp(duration)
local hours = duration / 3600
local minutes = duration % 3600 / 60
local seconds = duration % 60
return string.format("%02d:%02d:%02.03f", hours, minutes, seconds)
end
when the duration is 4.404 sec it returns 00:00:4.404
what is am looking for is 00:00:04.404
It should be:
string.format("%02d:%02d:%06.3f", hours, minutes, seconds)
Field width contains all characters of the number, including point and fraction.
Related
I'm making a program that calculates minutes and hours into seconds but it won't print, would appreciate any help
enter_hours = int(input("Please enter number of hours: "))
enter_minutes = int(input("Please enter number of minutes: "))
def CalculateSeconds():
hours = enter_hours * 3600
minutes = enter_minutes * 60
return(hours, minutes)
Figured it out
def CalculateSeconds(enter_hours, enter_minutes):
hours = enter_hours * 3600
minutes = enter_minutes * 60
return(hours, minutes)
seconds = CalculateSeconds(enter_hours=int(input("Enter first number: ")),
enter_minutes=int(input("Enter second number: ")))
print(seconds)
Below is an example of a countdown timer for Corona SDK written in LUA.
How would I add days, months and years to this?
local function updateTime()
-- decrement the number of seconds
secondsLeft = secondsLeft - 1
-- time is tracked in seconds. We need to convert it to minutes and seconds
local minutes = math.floor( secondsLeft / 60 )
local seconds = secondsLeft % 60
-- make it a string using string format.
local timeDisplay = string.format( "%02d:%02d", minutes, seconds )
clockText.text = timeDisplay
end
Days (and hours) would be trivial, but what about months and years? Since you have no timestamp telling us of how many seconds left to what, it's hard knowing exactly how many months, depending on the length of the months (28, 29, 30 or 31 days). The same with years if we consider leap years as well. In any case, here's something that might be sufficient:
local SECONDS_IN_HOUR = 60 * 60
local SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR
local SECONDS_IN_MONTH = 30 * SECONDS_IN_DAY -- assuming an average of 30 days per month
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY
local years = math.floor((secondsLeft / SECONDS_IN_YEAR) % 365)
local months = math.floor((secondsLeft / SECONDS_IN_MONTH) % 12)
local days = math.floor((secondsLeft / SECONDS_IN_DAY) % 30)
local hours = math.floor((secondsLeft / SECONDS_IN_HOUR) % 24)
local minutes = math.floor((secondsLeft / 60) % 60)
local seconds = secondsLeft % 60
The task is to Write a method that will take in a number of minutes, and returns a string that formats the number into hours:minutes.
here's what I have so far:
def time_conversion(minutes)
minutes = (minutes / 60) % 60
hours = minutes / (60 * 60)
format(" %02d:%02d ", hours, minutes)
return format
end
it's not working out for me
Try this
def time_conversion(time)
minutes = time % 60
hours = time / 60
minutes = (minutes < 10)? "0" + minutes.to_s : minutes.to_s
return hours.to_s + ":" + minutes
end
Using division in Ruby returns a whole number, lowered to the previous number. Using modulus returns the remainder after division.
Ruby's Numeric#divmod is exactly what you want here. It returns both the quotient and remainder of a division operation, so e.g. 66.divmod(60) returns [ 1, 6 ]. Combined with sprintf (or String#%, it makes for an extremely simple solution:
def time_conversion(minutes)
"%02d:%02d" % minutes.divmod(60)
end
puts time_conversion(192)
# => 03:12
Well try
h = minutes/60
M = minutes%60
My Timer is displaying Minutes and Hours, but once it gets to 60 minutes it restarts from 0 Minute.
Should I get rid of the modulo ( % 60 ) for minutes.
I would like my timer to display for ex: 80:45 ( basically not stopping at 60 min once it reaches 1 hour)
var min = 0
var sec = 0
func stringFromTimeInterval(interval: NSTimeInterval) -> String {
let interval = Int(interval)
let seconds = interval % 60
let minutes = (interval / 60) % 60
//let hours = (interval / 3600)// I don't need the hours
return String(format: "%02d:%02d",minutes, seconds)
}
% 60 means that it will spit out a minutes value that is the remainder when divided by 60(minutes). This is most probably because for time in the form hh:mm, you want it to go from 5:59 to 6:00, not 5:60. So changing the following line will give you what you seek.
let minutes = (interval / 60) % 60 -> let minutes = interval / 60
I'm attempting to convert MPH into minute miles. I'm currently running code to do this by doing 60 / the miles per hour which gives me the result in minute miles.
For example 60/8mph = 7.5
However the answer I get I need to convert into minutes and seconds so that I would have 7 minutes 30 seconds. Is there a way I can get the numbers after the decimal point so I can multiply it by 60 to convert it to seconds, then add it back to the minutes.
You can use remainder,
double remainder = fmod(a_double, another_double);
should include <math.h>
Well, I don't know whether there is an existing class that handles this, but to answer your specific question, the fractional part of the decimal (mantissa?) would be:
((60 % 8) / 8.0f)
You can multiply that by 60.
Do it in seconds...
3600/8 = 450
450/60 = 7 remainder 30
= 7:30
It's pretty simple, you're on the right path actually.
What you need to do is:
Get Minutes
Get Seconds
Convert seconds from int to real time (0.5 to 30, etc..)
Add seconds to minutes
Get minutes by casting it to an Integer:
int minutes = 60/8;
Get seconds by using the remainder:
float seconds = 60%8;
Convert seconds to real time:
int realSeconds = seconds * 60;
Now get result back by adding both:
int totalSeconds = minuts + realSeconds;
Here's a little function that does it (typed directly to browser, probably won't compile)
#include <math.h>
int getMinuteMiles(float mph){
int minutes = 60/mph;
double seconds = fmod(60, mph);
int realSeconds = seconds * 60;
return minutes+realSeconds;
}