Last active
February 13, 2024 19:37
-
-
Save vpsteinmann/959aa215608b6e2f0e9ae5446b621689 to your computer and use it in GitHub Desktop.
Person height formatting in Swift
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
// | |
// Created by Vincent Steinmann on 13.02.24. | |
// | |
import Foundation | |
let height = Measurement(value: 196, unit: UnitLength.centimeters) | |
// German in Germany. Prints 196 cm (with space) | |
print( | |
height.formatted( | |
.measurement( | |
width: .narrow, | |
usage: .personHeight | |
) | |
.locale(.init(components: .init( | |
languageCode: .german, | |
languageRegion: .germany | |
))) | |
) | |
) | |
// English in US. Prints 6′ 5.2″ | |
print( | |
height.formatted( | |
.measurement( | |
width: .narrow, | |
usage: .personHeight | |
) | |
.locale(.init(components: .init( | |
languageCode: .english, | |
languageRegion: .unitedStates | |
))) | |
) | |
) | |
// Convert current Locale to Locale.Components and manually set measurement system to metric | |
var customMeasurementSystemLocaleComponents = Locale.Components(locale: .current) | |
customMeasurementSystemLocaleComponents.measurementSystem = .metric | |
let customMeasurementSystemLocale = Locale(components: customMeasurementSystemLocaleComponents) | |
// Current locale with measurement system set to metric. | |
print( | |
height.formatted( | |
.measurement( | |
width: .narrow, | |
usage: .personHeight | |
) | |
.locale(customMeasurementSystemLocale) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment