C# Numeric Data Type

Numeric Datatype Method and Properties:

Parse: Converts a string to an int

string rollNoString = “123”;

int rollNo = int.Parse(rollNoString); // rollNo is 123

Parse: Converts a string to a float.

string weightString = “55.5”;

float weight = float.Parse(weightString); // weight is 55.5f

Parse: Converts a string to a double.

string piString = “3.14”;

double piValue = double.Parse(piString); // piValue is 3.14

Parse: Converts a string to a decimal.

string balanceString = “19.99”;

decimal balance = decimal.Parse(balanceString); // balance is 19.99m

Converts a double number to a string.

double piValue = 3.14159;

string piStringValue = piValue.ToString();

Console.WriteLine(“ToString() output: ” + piStringValue); // Output: “ToString() output: 3.14159”

GetType(): Returns the type of the current variable value and you have to use type as a data type to get type of that value.

double piValue = 3.14159;

Type doubleType = piValue.GetType();

Console.WriteLine(“GetType() output: ” + doubleType); // Output: “GetType() output: System.Double”

Equals(): Checked whether the first variable value equals to second and we have to use Boolean data type to check value is equal or not if both variables are equal answer is true otherwise false.

string weightstring= “3.14”;

double weight1 = double.Parse(weightstring);

double weight2 = 3.14;

bool isEqual =weight1.Equals(weight2);

Console.WriteLine(“Equals() output: ” + isEqual); // Output: “Equals() output: True

CompareTo(): Compares the number1 variable value to the number2 variable value and returns an integer that indicates whether the height1 value is less than, equal to, or greater than the height2 value and you have to use integer data type for the result.

double height1= 1.14;

double height2= 2.71;

int result =height1.CompareTo(height2);

Console.WriteLine(“CompareTo() output: ” + result); // Output: “CompareTo() output: -1” (since number1 is less than number2)

Mathematical Operations

int a = 10;

int b = 5;

Course Video

Course Video In English

Task:

       1. Write a program to convert the following string to an integer using the Parse.

       2. Write a program to convert the following string to a float using the Parse.

       3. Write a program to convert the following string to a double using the Parse.

       4. Write a program to convert the following string to a decimal using the Parse.

       5. Write a program to convert any double value to a string using the ToString method and print the result:

       6. Write a program to get and print the type of any variable using the GetType method:

       7. Write a program to check whether the following two double values are equal using the Equals method and print the result:

       8. Write a program to compare the following two double values using the CompareTo method and print the result:

       9. Write a program to perform the following mathematical operations on the given integers and print the results:

    int a = 10; int b = 5;
  • Subtraction
  • Addition
  • Multiplication
  • Division
Frequently Asked Questions

Still have a question?

Let's talk

Numeric data types in C# represent numbers and are divided into integral types (like int, long, short) and floating-point types (like float, double, decimal).

int is a 32-bit signed integer type, while long is a 64-bit signed integer type, used for storing larger integer values.

The default value of numeric data types in C# is 0 for integral types and 0.0 for floating-point types like float and double.

Use float when you need to save memory and precision is less critical. double offers higher precision and is more commonly used for floating-point calculations.

  • float: 32-bit single-precision floating-point number.
  • double: 64-bit double-precision floating-point number (higher precision than float).
  • decimal: 128-bit number, used for financial and monetary calculations requiring high precision.

If a numeric value exceeds its type’s range, it will cause an OverflowException unless explicitly checked or handled.

Unsigned numeric types, such as uint, long, and short, only represent non-negative values (positive integers or zero).

The decimal data type is specifically designed for high-precision decimal arithmetic, often used in financial applications.

You can convert numeric types using implicit or explicit casting. For example, int to float can be done implicitly, while float to int requires explicit casting.

Sure! Here are some examples:
int age = 25;
float price = 99.99f;
double distance = 12345.678;
decimal salary = 50000.75m;

Yes, Iqra Academy offers free online tutorials in Hindi that cover numeric data types in C# with practical examples.

Understanding numeric data types is crucial for handling calculations, ensuring proper memory allocation, and preventing errors in numerical operations.

You can practice by writing small programs to store and manipulate numbers using different data types like int, float, double, and decimal. You can also solve task-related questions for better practice.

Chatbot