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
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.
- (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.
- (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.
- (NSString *)stringValue: It is used to store the strings in the program and the output is also the string.
- (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.
- (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:
- CEIL(): It will return the smallest integer value larger than or equal to a given number.
- COS(): It will return the cosine of a number.
- COT(): It will return the cotangent of the number.
- DEGREES(): It converts a radian value into degrees.
- FLOOR(): It returns the largest integer value that is less than or equal to a number.
- MOD(): It returns the remainder of n divided by x.
- POWER(): It returns m raised to the nth power.
- RAND(): It returns a random number.
- 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:
Â
 So, the output of the above problem is 60.
Similar Reads
Literals in Objective-C Literals are the values that are assigned to a variable and which remain constant over the whole program. Literals are the values that cannot be modified in the program after declaration. Literals values are taken the same as variable values but their values cannot be altered in a program during its
6 min read
Interface in Objective-C Objective-C is an object-oriented programming language that was developed by Apple Inc. for its operating systems. One of the fundamental concepts of object-oriented programming is the ability to define interfaces, which specify the methods and properties that an object must have in order to be cons
7 min read
Functions in Objective-C Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and
8 min read
Constants in Objective-C Constants in Objective-C are values that cannot be modified once they are set. They are used to store a variety of data types, including numbers, strings, and booleans. Constants are a fundamental feature of the Objective-C programming language and are widely used in the development of applications
4 min read
Type Casting in Objective-C Objective-C is a programming language that was created in the 1980s and is widely used for developing software for the macOS and iOS platforms. One of Objective-C's key features is its ability to perform typecasting. Type casting enables programmers to convert one data type to another, which is usef
4 min read
Blocks in Objective-C Blocks in Objective-C are a potent tool that can simplify the development process. Blocks are an alternative to traditional functions, allowing you to write code that is more concise and efficient. They are used for many different purposes, including as callbacks and iterators. Blocks can also be us
6 min read
Arrays in Objective-C An Array is a data structure that holds similar types of data in contiguous memory. The Objective-C array is a single variable array that is used to store different types of elements in the array. Also, we can access the objective-c array using an index. It is quite similar to the C programming lang
5 min read
Log Handling in Objective-C Objective-C is a programming language commonly used for developing applications on Apple's macOS, iOS, and iPadOS platforms. One important aspect of programming in Objective-C is handling logs. Logging is the process of recording information about the state of a program or system in a log file, whic
6 min read
Strings in Objective-C Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the
4 min read
File Handling in Objective-C File handling is an essential part of any programming language, and Objective C is no exception. It allows developers to create, read, update, and delete files on the file system. This can be useful for a wide range of applications such as storing user data, saving game progress, or logging applicat
9 min read