Created
August 1, 2023 21:30
-
-
Save BrunoCerberus/4b57877755575af361c3b1c8fd3b978a to your computer and use it in GitHub Desktop.
MeasurementFormatter
This file contains hidden or 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
import Foundation | |
// Define the distance in kilometers | |
let distanceInKilometers = 1.2 | |
// Print the distance in kilometers | |
print("Distance is", distanceInKilometers, "km") | |
// Create a Measurement instance with the distance value and unit in kilometers | |
let measurement = Measurement( | |
value: distanceInKilometers, | |
unit: UnitLength.kilometers | |
) | |
// Create a MeasurementFormatter instance to format the measurement | |
let formatter = MeasurementFormatter() | |
// Set the locale to "en_US" to use the U.S. English number and date formatting conventions | |
formatter.locale = Locale(identifier: "en_US") | |
// Print the formatted distance using the MeasurementFormatter | |
print("Distance is", formatter.string(from: measurement)) | |
// Now, it's gonna show in Kilometers | |
formatter.locale = Locale(identifier: "pt_BR") | |
// Print the formatted distance using the MeasurementFormatter | |
print("Distance is", formatter.string(from: measurement)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment