Skip to content

Instantly share code, notes, and snippets.

@markiv
Last active February 4, 2020 15:06
Show Gist options
  • Save markiv/9962089118f9df9ddeec409c65c44ff2 to your computer and use it in GitHub Desktop.
Save markiv/9962089118f9df9ddeec409c65c44ff2 to your computer and use it in GitHub Desktop.
import Foundation
extension String.StringInterpolation {
fileprivate static let myMeasurementFormatter: MeasurementFormatter = {
let this = MeasurementFormatter()
this.unitOptions = [.naturalScale]
this.numberFormatter.maximumFractionDigits = 1
return this
}()
mutating func appendInterpolation<UnitType>(_ measurement: Measurement<UnitType>) where UnitType: Unit {
appendLiteral(Self.myMeasurementFormatter.string(from: measurement))
}
}
let distance = Measurement(value: 123456.78, unit: UnitLength.meters)
let temperature = Measurement(value: 26.78, unit: UnitTemperature.celsius)
let area = Measurement(value: 1234567890.12, unit: UnitArea.squareMeters)
print("Locale: \(Locale.autoupdatingCurrent)")
print("Distance: \(distance)")
print("Temperature: \(temperature)")
print("Area: \(area)")
// Sample Output:
// Locale: en_US (autoupdatingCurrent)
// Distance: 76.7 mi
// Temperature: 80.2°F
// Area: 1,234.6 km²
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment