2

好的,所以我有一个自定义数字键盘,它可以在标签(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 但它崩溃了,因为它无法将 $ 符号识别为数字。有人可以帮我吗?或者有更好的方法来制作带有 $ 符号的自定义垫?

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

4

3 回答 3

6

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

看起来你错误地使用了NSNumberFormatter. 以下是您为货币设置数字格式的方法:

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

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

于 2012-06-04T23:40:33.247 回答
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];
于 2012-06-04T23:09:11.100 回答
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

还是我误解了你的问题?

于 2012-06-04T23:31:55.490 回答