2

Okay so I have a custom number pad that works and shows numbers as 0.00 in a label (numberField), now i need it to show $0.00.

NSString *digit = sender.currentTitle;
numberField.text = [numberField.text stringByAppendingString:digit];

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGroupingSeparator:@""];
[numberFormatter setMaximumIntegerDigits:4];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMaximumFractionDigits:2];

numberField.text = [numberField.text stringByReplacingOccurrencesOfString:@"." withString:@""];
NSDecimalNumber *currency = [[NSDecimalNumber decimalNumberWithString:numberField.text] decimalNumberByDividingBy: [NSDecimalNumber decimalNumberWithString:@"100"]];
NSString *numberFieldFormat = [numberFormatter stringFromNumber:currency];
numberField.text = numberFieldFormat;

I tried a $%1.2f but it crashes because it does not recognize the $ sign as a number. Can someone help me out here? Or have a better way of making a custom pad with $ sign?

**Edit I'm considering making numberField a hidden label (alpha 0) and placing a copy (numberField2) directly on top and running it through stringWithFormat. It works but i thought there may be a cleaner way of doing it.

iamruskie
  • 766
  • 4
  • 9
  • 22

3 Answers3

6

You don't want it to show $0.00. What if I'm in the UK? Wouldn't I then want it to show £0.00?

It looks like you're incorrectly using the NSNumberFormatter. Here's how you format a number for currency:

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *string = [f stringFromNumber:[NSNumber numberWithFloat:1234.56]];
[f release];

That will format things correctly, regardless of your locale. It'll use the correct currency sign, the correct thousands separator, and the correct decimal separator. Attempting to recreate this functionality yourself is a Bad Idea™.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 1
    ...and the correct position of the currency sign (`0,00 €` in germany) – Matthias Bauch Jun 04 '12 at 23:46
  • Yes that formats a number properly but I'm unable to implement it into the numberpad without a crash. I've tried everything. – iamruskie Jun 05 '12 at 05:05
  • @David why is it crashing? Also, [this answer](http://stackoverflow.com/a/5291342/115730) and [this answer](http://stackoverflow.com/a/4821091/115730) may help. – Dave DeLong Jun 05 '12 at 16:14
  • Okay I got it to convert but it only works on currencies with the 00.00 format(Ex: USA & Euro) but for like Germany (0,00) , it does not work. Primarily because my code formats it specifically to (0.00) format, and doesn't recognize commas. Also on the Japanese Yen, they're format doesn't include decimals, it's in (0,000) format. So my question is, how do i fix this? I'm getting the values from the title of the button pressed on a custom number pad(IE 1,2,3,4,etc) Thanks for your help thus far. – iamruskie Jun 06 '12 at 01:58
0

Try this:

NSString *digit = sender.currentTitle;
numberField.text = [numberField.text stringByAppendingString:digit];

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGrouping Separator:@""];
[numberFormatter setMaximumIntegerDigits:4];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMaximumFractionDigits:2];

numberField.text = [numberField.text stringByReplacingOccurrencesOfString:@"." withString:@""];
numberField.text = [numberField.text stringByReplacingOccurrencesOfString:@"$" withString:@""];
NSDecimalNumber *currency = [[NSDecimalNumber decimalNumberWithString:numberField.text] decimalNumberByDividingBy: [NSDecimalNumber decimalNumberWithString:@"100"]];
NSString *numberFieldFormat = [numberFormatter stringFromNumber:currency];
numberField.text = [NSString stringWithFormat:@"$%@", numberFieldFormat];
Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
  • No I tried that, still crashes it. I'm considering making numberField a hidden label (alpha 0) and placing a copy (numberField2) directly on top and running it through stringWithFormat. It works but i thought there may be a cleaner way of doing it. – iamruskie Jun 04 '12 at 23:16
  • -1 you should *never* format currency yourself like this. You should *always* have the `NSNumberFormatter` do it for you. – Dave DeLong Jun 04 '12 at 23:41
0

From my understanding you just want to append a $ to your string. You could do something like this..

....

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGrouping Separator:@""];
[numberFormatter setMaximumIntegerDigits:4];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMaximumFractionDigits:2];

NSDecimalNumber *currency = [[NSDecimalNumber decimalNumberWithString:numberField.text]      decimalNumberByDividingBy: [NSDecimalNumber decimalNumberWithString:@"100"]];

NSString *formatted = [[currency stringValue] stringByReplacingOccurrencesOfString:[currency stringValue] withString:[NSString stringWithFormat:@"$%@",[currency stringValue]]];

  NSLog(@"%@", formatted); // Prints $0.00

Or Am I misunderstanding your question?

skram
  • 5,314
  • 1
  • 22
  • 26