2013-05-09 294 views
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:@"%[email protected]",numberAsString]; 
    } 
} 

這是代碼IVE放在一起它格式化文本框的輸出轉換成貨幣 IM試圖找出這樣的輸出是怎麼做的格式使用的貨幣,格式化貨幣輸出

,如果有人類型在1625年它的格式爲0.1625 目前如果somone類型在50它將其格式爲0.50這是正確的,因爲它應該是一個計算器,這是一個計算器,這是在美元或pences人民的電價。

回答

0
  • (無效)textFieldDidEndEditing:(的UITextField *)文本框

{ 如果(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]; 
} 

}

0

爲了讓貨幣格式使用下面的代碼:

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

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

    return numberAsString; 
} 
+0

從我可以看到,不能被納入我的代碼 – 2013-05-09 13:35:25