2009-02-13 60 views

回答

21

您需要使用数字格式器。注意:这也是你将如何显示日期/时间等以正确的格式,为用户区域设置

// 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

小数/末美分可以控制如下:

[currencyStyle setMaximumFractionDigits:0]; 

这里的文档链接从apple

3

答案上面会转换为数字,但这里是真正转换的NSString货币格式的方法

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