Begin statistics creation
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
import Foundation
|
||||
import _math
|
||||
|
||||
extension Double {
|
||||
|
||||
func rounded(to interval: Double) -> Double {
|
||||
(self / interval).rounded() * interval
|
||||
}
|
||||
|
||||
func rounded(decimals: Int) -> Double {
|
||||
let factor = _math.pow(10.0, Double(decimals))
|
||||
return (self * factor).rounded() / factor
|
||||
}
|
||||
}
|
||||
|
||||
25
CHDataManagement/Extensions/TimeInterval+Extensions.swift
Normal file
25
CHDataManagement/Extensions/TimeInterval+Extensions.swift
Normal file
@@ -0,0 +1,25 @@
|
||||
import Foundation
|
||||
|
||||
extension TimeInterval {
|
||||
|
||||
func duration(locale: Locale) -> String {
|
||||
let totalMinutes = Int((self / 60).rounded(to: 5))
|
||||
let hours = totalMinutes / 60
|
||||
let minutes = totalMinutes % 60
|
||||
|
||||
let suffix = locale.identifier.hasPrefix("de") ? "Std" : "h"
|
||||
|
||||
return String(format: "%d:%02d ", hours, minutes) + suffix
|
||||
}
|
||||
|
||||
func length(roundingToNearest interval: Double) -> String {
|
||||
let rounded = Int(self.rounded(to: interval))
|
||||
return "\(rounded) m"
|
||||
}
|
||||
|
||||
func energy(roundingToNearest interval: Double) -> String {
|
||||
let rounded = Int(self.rounded(to: interval))
|
||||
return "\(rounded) kcal"
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user