Questions tagged [measurementformatter]

13 questions
11
votes
3 answers

MeasurementFormatter to display feet and inches

I have been playing with MeasurementFormatter to try and display imperial lengths as 10'6" or 10 ft 6 in unsuccessfully. LengthFormatter does this correctly when isForPersonHeightUse is set to true, but it does not adapt to the user's locale well…
jjatie
  • 5,152
  • 5
  • 34
  • 56
10
votes
2 answers

Choosing units with MeasurementFormatter

This is similar to a question I asked yesterday but the answer I got doesn't seem to work in this case. I'm getting altitude values in meters from Core Location. I want to display these in a localized form. As an example, the altitude where I am…
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
7
votes
1 answer

Measurement Formatter: space between value and unit

I have a Measurement attribute: let measurement = Measurement(value: 10000.0, unit: UnitMass.grams) that I want to print as a string. I use the following MeasurementFormatter: let measurementFormatter =…
ajrlewis
  • 2,968
  • 3
  • 33
  • 67
6
votes
4 answers

How can I get the localised Unit Symbol from a MeasurementFormatter result?

I'm trying to get the localised unit symbol for any measurement obtained using a measurement formatter. For example, if the locale uses degrees Fahrenheit, but the app stores it's data in Celsius, I would create a measurement formatter as follows:…
Danny Bravo
  • 4,534
  • 1
  • 25
  • 43
3
votes
1 answer

Setting a locale of MeasurementFormatter instance voids any numberFormatter property options set on the instance

Solved Thanks to Martin R this has been solved by having to put the locale first. So: import Foundation let formatter = MeasurementFormatter() formatter.locale = Locale(identifier: "en_GB") // Set locale…
2
votes
1 answer

Zero symbol in iOS MeasurementFormatter

I'm having problems to declare/use a zero symbol for an unknown value when using MeasurementFormatter: let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .decimal numberFormatter.zeroSymbol = "?" numberFormatter.string(from: 0.0)…
2
votes
1 answer

MeasurementFormatter not working as intended

let userDefined = Measurement(value: Double(userInput.text!)!, unit: UnitMass.kilograms) let calculatedValue = userDefined.converted(to: UnitMass.grams) print(calculatedValue) let formatter = MeasurementFormatter() formatter.locale =…
Big Alex
  • 23
  • 4
2
votes
1 answer

Use MeasurementFormatter to display meters as feet

I'm having trouble getting the new MeasurementFormatter class to give me results in appropriate units. I have values in meters that I want to display in localized strings, either meters or feet depending on the user's locale. I have this: let meters…
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
1
vote
1 answer

Creating a custom FormatStyle for all Measurement Units

I have a few measurements (like UnitLength, UnitDuration, UnitSpeed etc) and for most of them I want to have a formatter that uses the provided unit (formatter.unitOptions = .providedUnit). To simplify the call site, I created a custom FormatStyle -…
phi
  • 10,634
  • 6
  • 53
  • 88
1
vote
0 answers

Localization for Unit

extension UnitSpeed { static var kilometersPerMinute: UnitSpeed { return UnitSpeed( symbol: "km/min", converter: UnitConverterLinear(coefficient: 16.666666666667) ) } } let formatter =…
Alexey Golikov
  • 652
  • 8
  • 19
0
votes
0 answers

formatted temperature measurement giving incorrect value

I am trying to show the temperature from weatherkit in a text field. If I use 'temp.formatted(.measurement(width: .abbreviated, usage: .weather)' for -0.31C it shows a weird value of -33F. It works for positive celsius values, only the negative ones…
Sekar
  • 31
  • 4
0
votes
1 answer

Swift measurementFormatter from Celsius not working

I am just starting with swift. This is what I have in my playground... let tempF1 = Measurement(value: 98.6, unit: UnitTemperature.fahrenheit) let tempC1 = tempF1.converted(to: UnitTemperature.celsius) let convertedTemperature1 =…
Sekar
  • 31
  • 4
0
votes
1 answer

Figure out which UnitType (liter / gallon) is the systems default

Is there any way to figure out which UnitType is the systems default based on the systems locale? I have an UITextField where a user can input an amount of water. I want to figure out if the input is in liters or gallons, so I can setup a correct…
patrickS
  • 3,590
  • 4
  • 29
  • 40