Open In App

Numbers in Objective-C

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Normally we work with numbers in almost every programming language. But in all other programming languages when we work with numbers we use primitive data types like int, short, long, float, etc. in Objective – C programming numbers have a very wide range we store the basic data types always in the form of objects. So by storing the basic data types in objects the range of numbers will increase. In it, there are various ranges of numbers. So It basically provides a wide range of methods to work with Numbers with the help of (NSNumbers).

So, basically, in short, we can say that in Objective – C programming language all the basic datatypes of numbers will be saved in the form of objects. and It also provides a range of methods also to work with numbers.

In addition to the various functions we can also create, Objective – C also includes some useful functions that we can use. These functions are available in standard Objective – C libraries and are called built-in functions. These are functions that can be included in your program and then use. Objective – C has a rich set of mathematical operations, which can be performed on various numbers.

Syntax:

data_type number1 = [name data_type];

data_type number2 = [name data_type];

– (NSNumber *)operations name:(NSNumber *)with:(NSNumber *);

Let’s take one example to understand the concept of numbers, Here in this example, we are going to store two numbers in the form of objects and then in the output, we are printing the sum of these two numbers by storing their output in the object let’s see the example.

Example 1:

ObjectiveC




#import <Foundation/Foundation.h>
 
@interface SampleClass:NSObject
- (NSNumber *)addA:(NSNumber *)a withB:(NSNumber *)b;
@end
// defining a functions that is used for the addition
@implementation SampleClass
 
- (NSNumber *)addA:(NSNumber *)a withB:(NSNumber *)b {
   float number1 = [a floatValue]; // declariing first number
   float number2 = [b floatValue]; // declaring second number
   float sum = number1 + number2; // defining sum variable that will be the adddition of two numbers
   NSNumber *result = [NSNumber numberWithFloat:sum];
   return result;
  // the result will be returned
}
 
@end
 
int main() {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
   SampleClass *sampleClass = [[SampleClass alloc]init];
   NSNumber *a = [NSNumber numberWithFloat:10.5]; // giving the first number input
   NSNumber *b = [NSNumber numberWithFloat:10.0]; // giving the second number input 
   NSNumber *result = [sampleClass addA:a withB:b];
   NSString *resultString = [result stringValue];
   NSLog(@"The sum is %@",resultString);
 
   [pool drain];
   return 0;
}


So here in this program, we are taking two numbers “10” and “10.5” and adding both of them to see the concept of the number system and their working in Objective – C by using the Concept of NSNumbers.

Let’s see the output of the program.

Output:

output

Output

So in the output, the sum of two numbers is printed. 

Now let’s see the various methods used in Objective – C to work with numbers with the help of NSNumber as one thing in common every number will be stored in the form of an object only.

  1. (BOOL) bool value: It is used to return the bool value to the user when the comparison between any two values will be “0” or “1” then it returns the bool value.
  2. (float) float value: It is used to store float value in the program in the form of an object or the output will also be in the float value.
  3. (NSString *)stringValue: It is used to store the strings in the program and the output is also the string.
  4. (int)intValue: It is used to store integer values in the program and the output is also the integer that will be received by the user.
  5. (NSNumber *)numberWithFloat:(float)value: It is used to create and returns an NSNumber object containing a given value, treating it as a float.

Lets we see the names of different functions that are used in tables but they will work on the numbers. Let’s see some of them:

  1. CEIL(): It will return the smallest integer value larger than or equal to a given number.
  2. COS(): It will return the cosine of a number.
  3. COT(): It will return the cotangent of the number.
  4. DEGREES(): It converts a radian value into degrees.
  5. FLOOR(): It returns the largest integer value that is less than or equal to a number.
  6. MOD(): It returns the remainder of n divided by x.
  7. POWER(): It returns m raised to the nth power.
  8. RAND(): It returns a random number.
  9. TRUNCATE(): This doesn’t work for SQL Server. It yields 7.53635, which has been trimmed to two places right of the decimal point.

So, these are various functions or terms that are used in tables and all of them work with the numbers. So these are some of the methods that are used to work with the numbers or NSNumbers in Objective – C. 

Now we take one more example of two numbers and we have to subtract one number from the other and see the output. Let the first number be 100, and the second number take 40, and the result will be 100- 40 = 60.

Let’s see the program for this problem:

Example 2:

ObjectiveC




#import <Foundation/Foundation.h>
 
@interface SampleClass:NSObject
- (NSNumber *)subtractX:(NSNumber *)x withY:(NSNumber *)y;
@end
// defining a function that is used for the subtraction of two numbers
@implementation SampleClass
 
- (NSNumber *)subtractX:(NSNumber *)x withY:(NSNumber *)y {
   float num1 = [x floatValue]; // declaring the first number
   float num2 = [y floatValue]; // declaring the second number
   float res = num1 - num2; // result or subtraction of two numbers
   NSNumber *result = [NSNumber numberWithFloat:res];
   return result;
  // result will be shown here
}
 
@end
 
int main() {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
   SampleClass *sampleClass = [[SampleClass alloc]init];
   NSNumber *x = [NSNumber numberWithFloat:100.0]; // giving the value of first number
   NSNumber *y = [NSNumber numberWithFloat:40.0];  // giving the value of second number 
   NSNumber *result = [sampleClass subtractX:x withY:y]; // the result is calculated
   NSString *resultString = [result stringValue]; // result will be printed as the string
   NSLog(@"The res is %@",resultString);
// result will be shown
   [pool drain];
   return 0;
}


So here in this program we take two variables “x” and “y” and the result will be the “x-y” let’s see the output.

x = 100.0 , y = 40.0

res = x – y => 60.0

Output:

Output

 

 So, the output of the above problem is 60.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads