Get value of localized Unit from Measurement

measurementformatter rounding
swift measurement
swift convert meters to feet
swift unit length

I need to get the internationalized value of a Measurement.

let speed = Measurement(value: 15, unit: UnitSpeed.kilometersPerHour)
let formatter = MeasurementFormatter()
formatter.string(from: speed) // is "9.32 mph"
speed.converted(to: .milesPerHour).value // this is what I want, but I have to hardcode the unit I am converting to

Can I get the value (9.32) without manually splitting strings and casting it? For temperatures you can use formatter.unitOptions = .temperatureWithoutUnit but this will stop the conversion and I would still end up with a string.

Is there anything like speed.converted(to: default).value to get the value of the localized Measurement?

All you really want to know is: What measurement system is the user using?

The answer has nothing to do with Measurement or MeasurementFormatter. The answer comes from the Locale. That's how MeasurementFormatter works. It consults the Locale. And so can you! Just say if Locale.current.usesMetricSystem and proceed according to the result.

Using MeasurementFormatter in Swift, When catering to a global audience, you should present this data in local units. As a developer, you should get into a habit of always using Foundation Most units of measure fall into one of three systems: British imperial, US Measurement(value: 3626.81, unit: UnitLength.miles) // You should collect  In doing so, we will also consider the actual range of values these quantities can assume, both in nature in general, and also within the subset of nature that chemistry normally addresses. In looking over the various units of measure, it is interesting to note that their unit values are set close to those encountered in everyday human experience

What I think you are after is the measurement system for the locale. The Mac currently understands three systems: metric, US and UK. The UK uses metric for lengths and weights but still uses miles/mph for road distances and speeds, hence the third option here – the usesMetricSystem property only supports two options and returns true for the UK.

To get the measurement system in Swift requires using NSLocale, this extension will add it to Locale:

extension Locale
   var measurementSystem : String?
      return (self as NSLocale).object(forKey: NSLocale.Key.measurementSystem) as? String

For added "fun" Apple doesn't actually specify the values of this property, they give examples but not a full definition. You can get the known three values querying the property on three locales known to use them, e.g. you could add the following to the extension:

   static let metricMeasurementSystem = Locale(identifier: "fr_FR").measurementSystem!
   static let usMeasurementSystem = Locale(identifier: "en_US").measurementSystem!
   static let ukMeasurementSystem = Locale(identifier: "en_UK").measurementSystem!

Of course if Swift did support them it would probably define an enum for the possibilities, you could do that as well.


BTW: For those who think the UK is being awkward, the "US" system is only used in three countries: US, Myanmar and Liberia.

Localize Units of Measurement per User, Having units of measurement enables displaying data according to AIMMS automates the task of adopting the numeric values to the units preferred by the user. When you insert an identifier of type Quantity, you will get a wizard with  Creates and returns a localized string representation of the provided measurement. func string ( from : Unit) -> String Creates and returns a localized string representation of the provided unit of measure.

Thanks to @CRD i did it this way:

add an extention:

extension Locale{
   var measurementSystem : String?
       return (self as NSLocale).object(forKey: NSLocale.Key.measurementSystem) as? String


class func perfSpeedFormatOnlyDigits(_ kmh: Double) -> String {

   let numberFormatter = NumberFormatter()
   numberFormatter.maximumFractionDigits = 0

   let formatter = MeasurementFormatter()
   let kilometersPerHour = Measurement(value: kmh, unit: UnitSpeed.kilometersPerHour)
   formatter.locale = (Locale.current.measurementSystem != "Metric") ? Locale(identifier: "en_US") : Locale.current
   formatter.unitStyle = .long //you have to use long to replace the correct String
   formatter.unitOptions = .naturalScale
   formatter.numberFormatter = numberFormatter
   if Locale.current.measurementSystem != "Metric" {
      return formatter.string(from: kilometersPerHour).replacingOccurrences(of: formatter.string(from: UnitSpeed.milesPerHour), with: "")
   }else {
      return formatter.string(from: kilometersPerHour).replacingOccurrences(of: formatter.string(from: UnitSpeed.kilometersPerHour), with: "")

Units of Measurement, Units of Measurement. 01/24/2017; 2 minutes to read. In this article. Throughout the world things are measured using different units and scales. The most  Answer to: Differentiate between the true value and the measured value of a measurement By signing up, you'll get thousands of step-by-step

NSMeasurement, An NSMeasurement object represents a quantity and unit of measure. NSMeasurement objects are initialized with an Unit object and double value. You can use the MeasurementFormatter class to create localized string representations of  Overview. The measurements interface consists of three parts: The measurement API resource returns URIs and URI templates to collections of measurements, so that measurements can be queried according to various filter criteria.

Units and Measurement, A numeric quantity labeled with a unit of measure, with support for unit Work with primitive values and other fundamental types used throughout Cocoa. Definition of unit of measurement in the dictionary. Meaning of unit of measurement. What does unit of measurement mean? Information and translations of unit of measurement in the most comprehensive dictionary definitions resource on the web.

Measurements and Units in Foundation – Ole Begemann, Measurement(value: 5, unit: UnitLength.kilometers) is tedious to write and hard to read. It's always hard to find the right balance between  Each row of the m × n 2 sampling matrix, A, corresponds to localized random measurements taken by a sampling unit located at random coordinates on the [1, n] × [1, n] Cartesian grid covering the n × n pixel image.

  • Doesn't this sort of miss the point of Measurement and Dimension? The Measurement value is the value; it does not need to be converted to anything. It is not in any particular small-u unit, except accidentally as the Dimension has some underlying baseUnit. So this sounds like an x-y problem. What are you really trying to do?
  • @matt I want to show a slider where the user can select a speed and I want the minimum / maximumValue to be multiple of 5. So I want 15-50km/h and 10-30mph which I thought to achieve via converting 15km/h to whatever unit the user is using and rounding that to the nearest multiple of 5.
  • Unfortunately I think this is wrong due to the UK... What you need to look at is the measurementSystem key which Swift does not make available directly on Locale.
  • Thank you very much for this valuable addition! I added an extension usesKilometersPerHour comparing your measurementSystem String to "Metric" and now I use this instead of the usesMetricSystem in the solution of @matt