您的位置:首页 > 其它

使用NSLog方法格式化输出信息

2010-10-25 11:08 375 查看
以下是格式化int,float,double和long型的示例

//

// PrintFormat.h
// DataTypes
//
//

#import <Foundation/Foundation.h>

@interface PrintFormat : NSObject

{

}

-(void) print;

@end

实现部份

//
// PrintFormat.m
// DataTypes
//
//

#import "PrintFormat.h"

@implementation PrintFormat

int num=90;
float _num=90;
double number=700.000;
long mlong=30;

-(void) print{

NSLog(@"The value of integer num is %i", num); //for integer the format specifier is %i

NSLog(@"The value of Long number is %i", mlong); //for Long the format specifier is also %i

NSLog(@"The value of float num is %.2f", _num); //for float the format specifier is %f and we can restrict it to print only two decimal place value by %.2f

NSLog(@"The value of double num is %f", number); //for Double the format specifier is %f (double can be represented in %e ,%E (in this it will be in exponentail form as well as in %e) , %g ,%G )

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: