iOS 13 : Space grouping separator changed

Viewed 279

We use NumberFormatter class to format some amounts in the app. Since we migrated on iOS 13, it seems that the groupingSeparator space used for the Locale fr_FR changed.

We have tests on formatted strings amounts such as :

var amount = "1000"
XCTAssertEqual(amount.formattedValue, "1 000,00")

On iOS < 13, This test succeeds. But if we launch test on iOS > 13, this test will fail.

The reason is because the type of space used for the groupingSeparator property of NumberFormatter in iOS 13 has changed so 1 000,00 will not be equal to 1 000,00 according the space grouping separator which is used.

Any idea to always use the correct space grouping separator according the current iOS version ?

Edit : We found that the used space on iOS 13 is now the NARROW NO-BREAK SPACE

Edit 2 : The currencyGroupingSeparator has also changed with the NNBSP, all currency grouping separator have been replaced with this new one.

1 Answers

Ok finally found a solution :

  1. Defined an extension of String which replace all new spaces of the string by the original white space.

    public extension String {
        var originalWhiteSpaced: String {
                let narrowNonBreakingSpace = "\u{202F}"
                let nonBreakingSpace = "\u{00a0}"
    
            return self
                .replacingOccurrences(of: narrowNonBreakingSpace, with: " ")
                .replacingOccurrences(of: nonBreakingSpace, with: " ")
        }}
    

And use it in the tests :

func testIsFormattingOfAmountCurrencyCorrectWhenAmountIsNegative() {
    let actualResult = formatter.formatAmountCurrency(-123472).originalWhiteSpaced
    let expectedResult = "- 123 472,00 €"

    XCTAssertEqual(actualResult, expectedResult)
}
Related