Home Tutorial Iphone Examples Nslog Formatting the out put in NSLog function

 
 

Formatting the out put in NSLog function
Posted on: July 29, 2009 at 12:00 AM
You will learn how to display formatted output into the log message using the NSLog function.

You can easily format the values of int, float, double and long while using the NSLog function.

Here is the example of formatting int, float, double and long values.

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

#import <Foundation/Foundation.h>

@interface PrintFormat : NSObject

{

}

-(void) print;

@end

The code of implementation file. 

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

Related Tags for Formatting the out put in NSLog function:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.