Not sure if this is a bug in NSMeasurementFormatter or NSMeasurement. The formatter unitOption is set to NSMeasurementFormatterUnitOptionsNaturalScale. It seems to all work well unless the initial value is negative. My app initiated NSMeasurement with the double value -576 meters. The formatter then produced the string "-22,699 inches". If I initiate NSMeasurement with 576 meters, the formatter produces the string "630 yards" which is what I'd expect, except it should be -630 yards. Is this a bug? For now I detect if the initial value is negative, set it to positive, get the string, and then prepend a negative sign to it.
NSMeasurementFormatter bug?
I confirm your result (with slightly different values however):
I tried the following (in playground)
let negativeMeasurement = Measurement(value: -576, unit: UnitLength.meters)
let measurementFormatter = MeasurementFormatter()
let preferredLocaleIdentifier = UserDefaults.standard.object(forKey: "locale") ?? "en_US"
let preferredLocale = Locale(identifier: preferredLocaleIdentifier as! String)
measurementFormatter.locale = preferredLocale
let zValue = measurementFormatter.string(from: negativeMeasurement)
print("Provided Unit -576 meters", zValue)
measurementFormatter.unitOptions = .naturalScale
let tValue = measurementFormatter.string(from: negativeMeasurement)
print("Natural Scale -576 meters", tValue)
I get:
Provided Unit -576 meters -0.358 mi
Natural Scale -576 meters -22,677.165 in
If I replace by +576m, I get
Provided Unit 576 meters 0.358 mi
Natural Scale 576 meters 629.921 yd
I found similar question for negative currencies:
https://stackoverflow.com/questions/18057778/nsnumberformatter-display-brackets-on-negative-values
Good to get confirmation, thanks. Sure appears to be a bug.
Thanks to note the bug number for reference and close the thread.
Submitted as bug FB7519993.
I beliveve I found another bug in the formatter. It works fine for distance, but I'd expect the same for other measurements as well, such as area. Below is my code snippet showing the issue. When entering a value of 100 meters, the formatter correctly displays the distance in feet. However, the formatter displays the area as meters squared. I would expect feet squared.
// Area
double value = 100; // meters
NSMeasurement* measurement = [[NSMeasurement alloc] initWithDoubleValue:value unit:[NSUnitArea squareMeters]];
NSMeasurementFormatter* measurementFormatter = [NSMeasurementFormatter new];
measurementFormatter.unitOptions = NSMeasurementFormatterUnitOptionsNaturalScale;
NSLog(@"Area is:%@", [measurementFormatter stringFromMeasurement:measurement] );
// Distance
measurement = [[NSMeasurement alloc] initWithDoubleValue:value unit:[NSUnitLength meters]];
NSLog(@"Distance is:%@", [measurementFormatter stringFromMeasurement:measurement] );
Area is:100 m²
Distance is:328.084 ft
Exact, it seems surface are not converted !
Tested on Catalina. Both in playground and code.
Submitted as bug FB7519993.
Well done.
So should close the thread now.