0
- (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,则将其格式化为 0.50,这是正确的,因为它应该是一个计算器,它以美分或便士为单位计算人们的电费。

4

2 回答 2

0

要获取货币格式,请使用以下代码:

- (NSString*) getAmountInCurrencyFormatWithValue:(NSString*) valueString {

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
    NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:[valueString doubleValue]]];

    return numberAsString;
}
于 2013-05-09T13:01:21.787 回答
0
  • (void)textFieldDidEndEditing:(UITextField *)textField

{如果(文本字段1){

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

}

于 2013-05-09T23:39:34.593 回答