A Beginner's Guide to Working with Numbers in C#

Category > CSHARP || Published on : Tuesday, March 7, 2023 || Views: 135 || C# programming arithmetic operations math functions formatting output.


If you are new to C# programming, you may be wondering how to work with numbers in the language. This article will cover the basics of working with numbers in C# including basic arithmetic operations, using built-in math functions, and formatting output.

Working with numbers is a fundamental part of programming in any language, and C# is no exception. In this article, we will cover the basics of working with numbers in C# including basic arithmetic operations, using built-in math functions, and formatting output.

  1. Basic Arithmetic Operations

C# supports all the basic arithmetic operations you would expect, including addition, subtraction, multiplication, and division. The operators for these operations are as follows:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /

Here's an example of using these operators in C#:

int a = 10;
int b = 5;

int sum = a + b;        // sum = 15
int difference = a - b; // difference = 5
int product = a * b;    // product = 50
int quotient = a / b;   // quotient = 2

Note that when dividing two integers, C# will perform integer division and truncate any remainder. If you want to perform floating-point division, you can cast one or both operands to a floating-point type:

int a = 10;
int b = 3;

float quotient = (float)a / b; // quotient = 3.33333
  1. Built-in Math Functions

C# provides a wide range of built-in math functions that you can use to perform more complex operations. Here are a few examples:

  • Math.Abs: returns the absolute value of a number
  • Math.Sqrt: returns the square root of a number
  • Math.Pow: raises a number to a specified power
  • Math.Round: rounds a number to the nearest integer or specified number of decimal places

Here's an example of using these functions in C#:

int a = -10;
double b = 3.14159;

int absA = Math.Abs(a);         // absA = 10
double sqrtB = Math.Sqrt(b);    // sqrtB = 1.77245385091
double powB = Math.Pow(b, 2);   // powB = 9.86960440109
double roundedB = Math.Round(b, 2); // roundedB = 3.14
  1. Formatting Output

When working with numbers, you may want to format the output to make it easier to read. C# provides a variety of formatting options that you can use to control how numbers are displayed. Here are a few examples:

  • N: displays a number with a thousands separator
  • C: displays a number as currency
  • F: displays a number with a fixed number of decimal places

Here's an example of using these formatting options in C#:

int a = 10000;
double b = 3.14159;

string formattedA = a.ToString("N0"); // formattedA = "10,000"
string formattedB1 = b.ToString("C"); // formattedB1 = "$3.14"
string formattedB2 = b.ToString("F2"); // formattedB2 = "3.14"

In the above code snippet, we are using the ToString method to format the numbers as strings with the specified format string.

Conclusion

In this article, we covered the basics of working with numbers in C# including basic arithmetic operations, using built-in math functions, and formatting output. By mastering these fundamentals, you will be well on your way to writing more sophisticated C# programs that involve complex mathematical operations.