Skip to content

Instantly share code, notes, and snippets.

@BrunoCerberus
Created August 1, 2023 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrunoCerberus/4b57877755575af361c3b1c8fd3b978a to your computer and use it in GitHub Desktop.
Save BrunoCerberus/4b57877755575af361c3b1c8fd3b978a to your computer and use it in GitHub Desktop.
MeasurementFormatter
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