首页 > 代码库 > NSNumber格式化显示

NSNumber格式化显示

NSNumber格式化显示主要用到的函数是:NSNumberFormatter,使用方法如下。
   
    //第一种显示方式
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterPercentStyle;
    NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:0.5]];
    NSLog(@"Formatted number string:%@",string);

    //第二种显示方式
    NSNumberFormatter *hereNumFormatter = [[NSNumberFormatter alloc] init];
    [hereNumFormatter setNumberStyle:NSNumberFormatterPercentStyle];
    NSString *str = [hereNumFormatter stringFromNumber:[NSNumber numberWithDouble:0.3333]];
    NSLog(@"%@",str);