带有$符号的自定义数字键盘

时间:2012-06-04 23:03:19

标签: iphone xcode xcode4

好的,所以我有一个自定义数字键盘,可以在标签(numberField)中显示数字为0.00,现在我需要它显示$ 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;

我尝试了$%1.2f,但它崩溃了,因为它无法将$符号识别为数字。 有人可以帮帮我吗?或者有一个更好的方法来制作带有$ sign的自定义打击垫?

**编辑  我正在考虑将numberField设为隐藏标签(alpha 0)并将副本(numberField2)直接放在顶部并通过stringWithFormat运行它。它有效,但我认为可能有一种更清洁的方式。

3 个答案:

答案 0 :(得分:6)

您不希望它显示$0.00。如果我在英国怎么办?那我不想让它显示£0.00吗?

您似乎错误地使用了NSNumberFormatter。以下是格式化货币数字的方式:

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

无论您的语言环境如何,都会正确格式化。它将使用正确的货币符号,正确的千位分隔符和正确的小数分隔符。试图自己重新创建这个功能是一个坏主意。

答案 1 :(得分:0)

试试这个:

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];

答案 2 :(得分:0)

根据我的理解,你只想将$附加到你的字符串中。你可以这样做......

....

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

或者我误解了你的问题?