MeasurementFormatter not behaving correctly

Hi,


I have a problem with MeasurementFormatter not behaving correctly with the .naturalScale option.

I guess it's a problem in the code to switch units (for example, if value ≤ 1 unit {switch to this unit} being if value ≥ 1 {switch to next unit} )


For example, using the MeasurementFormatter to display 1 gram and 2 grams in a metric system, the result is "1 g" for 1 gram, but "0.002 kg" for 2 grams.

Also, depending on the locale, everything is in plural, so 1 gram is displayed as "1 grams".


A last thing (this is Suggestion rather than a bug), it would be good if we had more control over the formatter display, for example, limiting the number of decimals (this would solve the "0.002 kg" above, but also get nice results in localized apps without breaking the design.

Also, the behaviour of .providedUnit can be kept partially for metric/imperial systems, or SI system (this information is in Locale only). For example, in a recipe/health system, we want to keep international units (grams, micro grams, etc) even in an imperial system, while also getting the behaviour of .naturalScale to automatically convert values/units based on the value's scale.

One last control would be keeping the current unit until the value goes beyond the following unit, for example, write 999 grams as "999 grams", and not "0.999 kg", but allow 1001 grams to be displayed as "1.001 kg".


For Admins: I filled a rdar #35930311


I won't attach a playground, but will write the results here:

let formatter = MeasurementFormatter()
formatter.unitOptions = .naturalScale
formatter.locale = Locale(identifier: "jp")
formatter.string(from: Measurement(value: 1, unit: UnitMass.grams)) // Work correctly, result 1 g
formatter.string(from: Measurement(value: 2, unit: UnitMass.grams)) // Expected 2 gr, result 0.002 kg
formatter.string(from: Measurement(value: 1, unit: UnitLength.centimeters)) // Expected 1 cm, result 0.01 m
formatter.string(from: Measurement(value: 1, unit: UnitLength.millimeters)) // Work correctly, result 1 mm
formatter.string(from: Measurement(value: 2, unit: UnitLength.millimeters)) // Expected 2 mm, result 0.002 m
formatter.string(from: Measurement(value: 1, unit: UnitVolume.milliliters)) // Expected 1 mL, resul 0.1 cL
formatter.string(from: Measurement(value: 2, unit: UnitVolume.milliliters)) // Expected 2 mL, result 0.002 L

I'm still seeing this behavior in MeasurementFormatter. Did you ever get any response in your feedback issue @EinharchAltPlus?

MeasurementFormatter not behaving correctly
 
 
Q