- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textfield1) {
NSString *txt = self.textfield1.text;
double num1 = [txt doubleValue];
double tCost = num1 /100;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:tCost]];
self.textfield1.text = [NSString stringWithFormat:@"%0.@",numberAsString];
}
}
这是我把它组合在一起的代码,它将文本字段的输出格式化为货币 我试图弄清楚如何格式化货币,以便输出,
如果某人在1625年输入,则将其格式化为0.1625 目前,如果50中的somone类型将其格式化为0.50,这是正确的,因为它应该是一个计算器,它接受人民的电费,以美分或英镑。
答案 0 :(得分:0)
要获取货币格式,请使用以下代码:
- (NSString*) getAmountInCurrencyFormatWithValue:(NSString*) valueString {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:[valueString doubleValue]]];
return numberAsString;
}
答案 1 :(得分:0)
{ if(textfield1){
NSString *txt = self.textfield1.text;
double num1 = [txt doubleValue];
double tCost = num1/100;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
// NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:tCost]];
//NSString *numberAsString = [numberFormatter stringFromNumber:
// [NSNumber numberWithDouble:[tCost doubleValue]]];
[numberFormatter setMaximumFractionDigits:3];
[numberFormatter setRoundingMode: NSNumberFormatterRoundUp];
NSString *numberString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:tCost]];
self.textfield1.text = [NSString stringWithFormat:@"%@",numberString];
}
}