articles

Home / DeveloperSection / Articles / Objective-C : Numbers

Objective-C : Numbers

Tarun Kumar3131 09-Jul-2015

Previously we learn Constants in Objective C: Objective-C : Constants

 

In Objective-C programming language, in order to save the basic data types like int, float, bool in object form.

Objective-C provides a range of methods to work with NSNumber and important ones are listed in following table:

S.N.       

Method and Description

1

+ (NSNumber *)numberWithBool:(BOOL)value

Creates and returns an NSNumber object containing a given value, treating it as a BOOL.

2

+ (NSNumber *)numberWithChar:(char)value

Creates and returns an NSNumber object containing a given value, treating it as a signed char.

3

+ (NSNumber *)numberWithDouble:(double)value

Creates and returns an NSNumber object containing a given value, treating it as a double.

4

+ (NSNumber *)numberWithFloat:(float)value

Creates and returns an NSNumber object containing a given value, treating it as a float.

5

+ (NSNumber *)numberWithInt:(int)value

Creates and returns an NSNumber object containing a given value, treating it as a signed int.

6

+ (NSNumber *)numberWithInteger:(NSInteger)value

Creates and returns an NSNumber object containing a given value, treating it as an NSInteger.

7

- (BOOL)boolValue

Returns the receiver's value as a BOOL.

8

- (char)charValue

Returns the receiver's value as a char.

9

- (double)doubleValue

Returns the receiver's value as a double.

10

- (float)floatValue

Returns the receiver's value as a float.

11

- (NSInteger)integerValue

Returns the receiver's value as an NSInteger.

12

- (int)intValue

Returns the receiver's value as an int.

13

- (NSString *)stringValue

Returns the receiver's value as a human-readable string.

 

NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL. (Note that number objects do not necessarily preserve the type they are created with.) It also defines a compare: method to determine the ordering of two NSNumber objects.


Subclassing Notes

As with any class cluster, subclasses of NSNumber must override the primitive methods of its superclass, NSValue. In addition, there are two requirements around the data type your subclass represents:
  1. Your implementation of objCType must return one of “c”, “C”, “s”, “S”, “i”, “I”, “l”, “L”, “q”, “Q”, “f”, and “d”. This is required for the other methods of NSNumber to behave correctly.
  2. Your subclass must override the accessor method that corresponds to the declared type—for example, if your implementation of objCType returns “i”, you must override intValue. 

For example, we can extend our example to retrieve and display the value stored in our number object as follows:

#import <Foundation/Foundation.h>
int main()
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  NSNumber *myFloat;
  float floatvalue;

  myFloat = [NSNumber numberWithDouble: 10.09];
  floatvalue = [myFloat floatValue];
  NSLog (@"Value = %f", floatvalue);

  [pool drain];
  return 0;
}

Now when we compile and run the program, we will get the following result.

2015-07-09 03:09:24.786 demo[19264] Value = 10.090000


Note that the method used to retrieve the value stored in a number object must match the method used to store the object. An attempt, for example, to retrieve a char from an object initialized as double will provide an unexpected result.

 

Comparing Number Objects


To compare the values stored in two number objects it is necessary to use either the isEqualToNumber or compare methods. These are both instance variables, and as such are called on one object instance, passing through the second object as an argument.

isEqualToNumber returns a Boolean value depending on whether the two objects contain the same numbers. For example:

#import <Foundation/Foundation.h>
int main()
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  NSNumber *myFloat1;
  NSNumber *myFloat2;

  myFloat1 = [NSNumber numberWithDouble: 10.09];
  myFloat2 = [NSNumber numberWithDouble: 10.08];

  if ([myFloat1 isEqualToNumber: myFloat2])
        NSLog (@"Numbers are equal");
  else
        NSLog (@"Numbers are not equal");

  [pool drain];
  return 0;
}

Now when we compile and run the program, we will get the following result.

2015-07-09 03:13:02.438 demo[21162] Numbers are not equal

  

The compare method is used when you need to know whether one number is less than, greater than or equal to another when those numbers are held in number objects. The method returns the result of the comparison in the form of an NSComparisonResult enumeration. Possible settings are NSOrderedSame if the numbers are equal, NSOrderedAscending is the value stored in the first object is less than the number stored in the second and NSOrderedDescending if the opposite is true:

#import <Foundation/Foundation.h>
int main()
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  NSNumber *myFloat1;
  NSNumber *myFloat2;

  myFloat1 = [NSNumber numberWithDouble: 10.09];
  myFloat2 = [NSNumber numberWithDouble: 10.08];
  NSComparisonResult result;
  result = [myFloat1 compare: myFloat2];

  if (result == NSOrderedSame)
     NSLog(@"Numbers are equal");
  else if (result == NSOrderedAscending)
     NSLog(@"Float1 is less than Float2");
  else if (result == NSOrderedDescending)
     NSLog(@"Float1 is greater than Float2");
    
  [pool drain];
  return 0;
}

Now when we compile and run the program, we will get the following result.

2015-07-09 03:14:13.688 demo[21740] Float1 is greater than Float2

 

Getting the Number Object Value as a String

In order to convert the value stored in a number object to a string, the stringValue instance method is provided by the NSNumber class. This method returns the current value as a string object (for more information on string objects refer to Working with String Objects in Objective-C):


#import <Foundation/Foundation.h>
int main()
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  NSNumber *myFloat;
  NSString *myString;     

  myFloat = [NSNumber numberWithDouble: 10.09];
  myString = [myFloat stringValue];
  NSLog (@"Number as string is %@", myString);
    
  [pool drain];
  return 0;
}

Now when we compile and run the program, we will get the following result.

2015-07-09 03:16:07.085 demo[22825] Number as string is 10.09
 
NSNumber Literals


Previously:


NSNumber *number;

number = [NSNumber numberWithChar:'X'];

number = [NSNumber numberWithInt:12345];

number = [NSNumber numberWithUnsignedLong:12345ul];

number = [NSNumber numberWithLongLong:12345ll];

number = [NSNumber numberWithFloat:123.45f];

number = [NSNumber numberWithDouble:123.45];

number = [NSNumber numberWithBool:YES];



Now:


NSNumber *number;

number = @'X';

number = @12345;

number = @12345ul;

number = @12345ll;

number = @123.45f;

number = @123.45;

number = @YES;


Updated 30-Nov-2017

Leave Comment

Comments

Liked By