Search code examples
cocoansnumber

NumberFormatter Minimum Not Working Correctly?


I configure a basic NumberFormatter:

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .spellOut

Then I set the minimum variable:

numberFormatter.minimum = 1

Which, according to the documentation sets:

The lowest number allowed as input by the receiver.

But when I run the following conversion:

numberFormatter.string(from: -1)

I get the erroneous output:

minus one

Why isn't the minimum value being acknowledged?


Solution

  • What the documentation says with the misleading term input is, that it does not allow to convert a string with a value lower than the minimum value into a number. This is, because it is hard to test, whether a string has a lower value, esp. in a localized form

    You go the other way from a number to a string. For this scenario it is easy to test yourself, whether the boundaries are met. Therefore the number formatters does not check the boundaries.