7 Replies
      Latest reply on Jan 15, 2020 2:12 PM by Claude31
      KyleKai Level 1 Level 1 (10 points)

        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.

        • Re: NSMeasurementFormatter bug?
          Claude31 Level 8 Level 8 (7,895 points)

          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: