Last active
August 29, 2015 14:17
-
-
Save regnerjr/a2ca1dea30d0e0c44d32 to your computer and use it in GitHub Desktop.
A playground for calculating hours and minutes into fractional hours i.e. 4 hours and 12 minutes should be 4.2 hours.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A function for calculating the time in 0.1 hour increments | |
/// i.e. 1 hour and 6 min is 1.1hours, 3 hours 12 minutes is 3.2 hours | |
func calc(hours: Int, min: Int) -> Double { | |
let dbl = Double(min) | |
let roundedMin = (dbl / 60.0) | |
let flr = floor(roundedMin * 10 ) / 10 | |
return Double(hours) + floor(roundedMin * 10) / 10 | |
} | |
/// How to round a number? | |
/// Given some number of hours and minutes, we want to round those to be a decimal | |
/// representation of the hours worked in the week | |
//for example give 1 hour and 6 minutes we should have 1.1 hours | |
//let h = 1 | |
//let m = 6 | |
// | |
//let total = calc(h, m) | |
// What about 7 minutes, clearly that is closer to 1.1 hours than it is to 1.2 hours | |
//let h2 = 1 | |
//let m2 = 7 | |
// | |
//let t2 = calc(h2, m2) | |
/// how can we modify our calc function to round off the rest of those digits, | |
/// Lets make it easy on our selves and only look at 2 digits | |
/// Using the round function we may be able to get close | |
//let h3 = 2 | |
//let m3 = 11 //lets see if 11 gets rounded up? | |
// | |
//let t3 = calc(h3, m3) //sweet | |
/// Check the middle numbers to be sure | |
/// if 6 is the 0.1 and 12 is 0.2 , 9 will be right in the middle | |
/// So 8 should round down | |
//let h4 = 2 | |
//let m4 = 8 | |
//let t4 = calc(h4, m4) | |
/// and 9 will round up | |
//let h5 = 2 | |
//let m5 = 9 | |
//let t5 = calc(h5, m5) | |
// running the above function on a full hour worth of minutes we should see a nice stair step pattern with jumps at 6,12,18,etc. | |
let minutes = Array(1..<60) | |
let frac = map(minutes){ | |
calc(0, $0) | |
} | |
frac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment