A more elegant solution may also be the easiest.
You do not need a decimal separator
Why? Because you can just pull it out of user input. For example, in the US locale, when you need to enter $ 1.23, you start by entering the numbers 1-2-3 (in that order). In the system, as you enter each character, this will be recognized as:
- user enters 1: $ 0.01
- user enters 2: $ 0.12
- user enters 3: $ 1.23
Notice how we determined the decimal separator based on user input. Now, if the user wants to enter $ 1.00, they will simply enter the numbers 1-0-0.
In order for your code to process currencies in different language standards, you need to get the maximum digits in the currency. This can be done using the following code snippet:
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init]; [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; int currencyScale = [currencyFormatter maximumFractionDigits];
For example, the Japanese yen has a maximum fraction of 0. Therefore, when working with the yen, there is no decimal separator, and therefore you don’t even have to worry about fractional amounts.
This approach to the problem allows you to use the numeric input keyboard provided by Apple without the headaches of custom keyboards, validation, etc.
shek Nov 26 '08 at 18:03 2008-11-26 18:03
source share