C# Working With Numbers

Published on
3 mins read
––– views

Numbers in C#

In C#, numbers can be represented using different data types. For whole numbers, we use int, like -1,2,35 ....

When it comes to numbers with decimal places, such as 1.2, 23.2, -1.23, we have options like float, double, and decimal. The choice depends on the required precision.

In financial equations, like banking calculations, using decimal is the most suitable approach. It ensures that bank funds are not rounded up or down. If you're dealing with large amounts and performing numerous operations, high precision provided by decimal can be quite beneficial.

In many cases double is the preferred choice. It's more precise than float but also requires less memory and faster than decimal. decimal is less precise then decimal but high precision isn't always needed.

The choice of data type impacts performance, so it's crucial to consider the specific needs of your code.

Math

Like all programming languages, C# supports Math (yay!).

operations are the same

float firstNumber = 5;
float secondNumber = 2;

// firstNumber + secondNumber = 7;
// firstNumber - secondNumber = 3;
// firstNumber * secondNumber = 10;
// firstNumber / secondNumber = 2.5;
// firstNumber % secondNumber = 1;

The only thing you may not know is the % remainder operator, which is similar to division but outputs the remainder value. It's useful for determining if a number is odd or even.

For example, 10 % 3 would give a result of 1, because when we divide 10 by 3, we get 3 as the quotient and 1 as the remainder. In contrast, division / would output 3.33 as a floating-point result.

Build-in Methods

There are several build-in methods for more advanced math needs.

  • Math.Abs() will find the absolute value. Math.Abs(-5) will output 5.
  • Math.Sqrt() will find the square root of a number, Math.Sqrt(25) will output 5`.

  • Math.Floor() is used to round the given decimal down to nearest whole number. Math.Floor(5.9) will output 5.

  • Math.Ceiling() will round it up. Math.Ceiling(4.1)will return5`.

  • Math.Round() will decide depending on the number. Math.Round(1.5) or lower will return 1, but 1.6 (or higher) will return 2.

  • Math.Min() will return the smallest of two numbers. Math.Min(1,2) will return 1.

  • Math.Max(), will return the highest.

  • Math.Pow() will raise the number to specified power. Math.Pow(2,3) will return 8, (2 x 2 x 2).

  • Math.Truncate() will remove the decimal. Math.Truncate(5.2) will output 5. It doesn't perform any rounding or calculations. It simply removes it. For example, Math.Floor(-5.9) would output -6 but Math.Truncate(-5.9) would simply return -5. It can be useful in some financial calculations.

You can also combine multiple methods for the same variable.

int number = -2342;

double roundedSqr = Math.Floor(Math.Sqrt(number));

This line will take the square of the number variable and round it down to the nearest whole number. This line will return NaN, not a number. Because, we cannot take square root of a negative number.

no worries,

int number = -2342;
double numberTwoSqrt = Math.Floor(Math.Sqrt(Math.Abs(number)));

By using Math.Abs() inside the Math.Sqrt() we can convert a negative number to positive, take it's square and round it down.