Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out number of fraction digits in currency in iOS

I use NSNumberFormatter to format currencies in a localized way, and it works fine. But I want to override this and give the user the option to override the number of digits after the decimal separator.

How can I find out the number of digits the NSNumberFormatter will use for a certain currency? I have looked in the NSLocale object, but none of the keys tell me this.

NSString * const NSLocaleIdentifier;
NSString * const NSLocaleLanguageCode;
NSString * const NSLocaleCountryCode;
NSString * const NSLocaleScriptCode;
NSString * const NSLocaleVariantCode;
NSString * const NSLocaleExemplarCharacterSet;
NSString * const NSLocaleCalendar;
NSString * const NSLocaleCollationIdentifier;
NSString * const NSLocaleUsesMetricSystem;
NSString * const NSLocaleMeasurementSystem;
NSString * const NSLocaleDecimalSeparator;
NSString * const NSLocaleGroupingSeparator;
NSString * const NSLocaleCurrencySymbol;
NSString * const NSLocaleCurrencyCode;
NSString * const NSLocaleCollatorIdentifier;
NSString * const NSLocaleQuotationBeginDelimiterKey;
NSString * const NSLocaleQuotationEndDelimiterKey;
NSString * const NSLocaleAlternateQuotationBeginDelimiterKey;
NSString * const NSLocaleAlternateQuotationEndDelimiterKey;

How can I find out the correct number of decimals for a currency like the NSNumberFormatter seems to know?

like image 862
thejaz Avatar asked Feb 19 '23 07:02

thejaz


1 Answers

Create an NSNumberFormatter instance and set the currency code with the desired currency. You can then query the property maximumFractionDigits on the currency formatter.

like image 69
phoganuci Avatar answered Feb 23 '23 21:02

phoganuci