2019-01-16 23:26:11 +01:00
|
|
|
import Foundation
|
|
|
|
|
2019-10-25 18:08:35 +02:00
|
|
|
extension Double: RawRepresentable {
|
|
|
|
|
|
|
|
//MARK: - copied from https://github.com/LoopKit/LoopKit
|
|
|
|
|
|
|
|
public typealias RawValue = Double
|
|
|
|
|
|
|
|
public init?(rawValue: RawValue) {
|
|
|
|
self = rawValue
|
|
|
|
}
|
|
|
|
|
|
|
|
public var rawValue: RawValue {
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - own code
|
|
|
|
|
2019-01-16 23:26:11 +01:00
|
|
|
/// converts mgdl to mmol
|
|
|
|
func mgdlToMmol() -> Double {
|
2019-07-28 14:16:27 +02:00
|
|
|
return self * ConstantsBloodGlucose.mgDlToMmoll
|
2019-01-16 23:26:11 +01:00
|
|
|
}
|
|
|
|
|
2019-05-17 23:09:47 +02:00
|
|
|
/// converts mgdl to mmol if parameter mgdl = false. If mgdl = true then just returns self
|
2019-05-05 13:44:10 +02:00
|
|
|
func mgdlToMmol(mgdl:Bool) -> Double {
|
|
|
|
if mgdl {
|
|
|
|
return self
|
|
|
|
} else {
|
2019-07-28 14:16:27 +02:00
|
|
|
return self * ConstantsBloodGlucose.mgDlToMmoll
|
2019-05-05 13:44:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 23:09:47 +02:00
|
|
|
/// converts mmol to mgdl if parameter mgdl = false. If mgdl = true then just returns self
|
|
|
|
func mmolToMgdl(mgdl:Bool) -> Double {
|
|
|
|
if mgdl {
|
|
|
|
return self
|
|
|
|
} else {
|
|
|
|
return self.mmolToMgdl()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 23:26:11 +01:00
|
|
|
/// converts mmol to mgdl
|
|
|
|
func mmolToMgdl() -> Double {
|
2019-07-28 14:16:27 +02:00
|
|
|
return self * ConstantsBloodGlucose.mmollToMgdl
|
2019-01-16 23:26:11 +01:00
|
|
|
}
|
2019-02-23 11:27:38 +01:00
|
|
|
|
|
|
|
/// returns the value rounded to fractionDigits
|
2020-12-12 21:56:42 +01:00
|
|
|
func round(toDecimalPlaces: Int) -> Double {
|
|
|
|
let multiplier = pow(10, Double(toDecimalPlaces))
|
2019-02-23 11:27:38 +01:00
|
|
|
return Darwin.round(self * multiplier) / multiplier
|
|
|
|
}
|
|
|
|
|
|
|
|
/// takes self as Double as bloodglucose value, converts value to string, round. Number of digits after decimal seperator depends on the unit. For mg/dl 0 digits after decimal seperator, for mmol, 1 digit after decimal seperator
|
|
|
|
func bgValuetoString(mgdl:Bool) -> String {
|
|
|
|
if mgdl {
|
|
|
|
return String(format:"%.0f", self)
|
|
|
|
} else {
|
|
|
|
return String(format:"%.1f", self)
|
|
|
|
}
|
|
|
|
}
|
2019-03-16 18:28:38 +01:00
|
|
|
|
2020-12-12 21:56:42 +01:00
|
|
|
/// if mgdl, then returns self, unchanged. If not mgdl, return self rounded to 1 decimal place
|
|
|
|
func bgValueRounded(mgdl: Bool) -> Double {
|
|
|
|
|
|
|
|
if mgdl {
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return self.round(toDecimalPlaces: 1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-05 13:44:10 +02:00
|
|
|
/// converts mmol to mgdl if parametermgdl = false and, converts value to string, round. Number of digits after decimal seperator depends on the unit. For mg/dl 0 digits after decimal seperator, for mmol, 1 digit after decimal seperator
|
|
|
|
///
|
|
|
|
/// this function is actually a combination of mmolToMgdl if mgdl = true and bgValuetoString
|
|
|
|
func mgdlToMmolAndToString(mgdl:Bool) -> String {
|
|
|
|
if mgdl {
|
|
|
|
return String(format:"%.0f", self)
|
|
|
|
} else {
|
|
|
|
return String(format:"%.1f", self.mgdlToMmol())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 18:28:38 +01:00
|
|
|
/// treats the double as timestamp in milliseconds, since 1970 and prints as date string
|
|
|
|
func asTimeStampInMilliSecondsToString() -> String {
|
|
|
|
let asDate = Date(timeIntervalSince1970: self/1000)
|
|
|
|
return asDate.description(with: .current)
|
|
|
|
}
|
2019-04-23 17:24:05 +02:00
|
|
|
|
2019-01-16 23:26:11 +01:00
|
|
|
}
|
2019-02-23 11:27:38 +01:00
|
|
|
|
|
|
|
|