2009-02-13 37 views

Trả lời

21

Bạn cần sử dụng trình định dạng số. Lưu ý đây cũng là cách bạn sẽ hiển thị ngày/lần vv trong các định dạng chính xác cho người sử dụng Locale

// alloc formatter 
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init]; 

// set options. 
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]; 

NSNumber *amount = [NSNumber numberWithInteger:78000]; 

// get formatted string 
NSString* formatted = [currencyStyle stringFromNumber:amount] 

[currencyStyle release]; 
3

Số thập phân/cent ở cuối có thể được kiểm soát như sau:

[currencyStyle setMaximumFractionDigits:0]; 

Đây là tài liệu hướng dẫn liên kết từ apple

3

Câu trả lời ở trên sẽ chuyển đổi một số lượng nhưng đây là một phương pháp để thực sự chuyển đổi một NSString sang định dạng tiền tệ

- (NSString*) formatCurrencyWithString: (NSString *) string 
{ 
    // alloc formatter 
    NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init]; 

    // set options. 
    [currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4]; 

    // reset style to no style for converting string to number. 
    [currencyStyle setNumberStyle:NSNumberFormatterNoStyle]; 

    //create number from string 
    NSNumber * balance = [currencyStyle numberFromString:string]; 

    //now set to currency format 
    [currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]; 

    // get formatted string 
    NSString* formatted = [currencyStyle stringFromNumber:balance]; 

    //release 
    [currencyStyle release]; 

    //return formatted string 
    return formatted; 
} 
Các vấn đề liên quan