Andy Andy - 1 month ago 19
Objective-C Question

Format accessibility label for price

I use

NSNumberFormatter
to format price for voice over. I found that it works fine when no thousands separator used and for certain list of currencies such as US$, €, £, however it doesn't seem to work for any other currencies, e.g. CA$ is pronounced as C-A-Dollar... What's the right way to configure
NSNumberFormatter
to get a decent output suitable for voice over?

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.usesGroupingSeparator = NO;
formatter.currencyCode = @"NOK";

view.accessibilityLabel = [formatter stringFromNumber:@100];


As Carl mentioned below,
.CurrencyPluralStyle
can be used. It formats currency based on locale and ignores all other settings even
usesGroupingSeparator
. With some manual work it can be a suitable alternative!

Example below:

let formatter = NumberFormatter()
formatter.numberStyle = .currencyPlural
formatter.usesGroupingSeparator = false // being entirely ignored :/
formatter.locale = Locale(identifier: "en_CA")

let str = formatter.string(from: 1000.50)!

print("\(str)")


Produces:


1,000.50 Canadian dollars

Answer

Does NSNumberFormatterCurrencyPluralStyle meet your needs? That is iOS9 and later only, though.

Comments