SoFunction
Updated on 2025-03-07

Detailed explanation of examples of commonly used mathematical operations in C# Math

Zero, dynamic library

Introduce dynamic library using;

Math provides constant and static methods for general mathematical functions, logarithmic functions, trigonometric functions, etc., which is very convenient to use. Here are a few commonly used ones.

1. Discard decimal places

1. Rounding ()

(74.5, ) = 75; // Reserved to individual positions(-74.5, ) = -75; // When the target value is negative, rounding and positive integers are the same

The value obtained by simply using (4.5) is 4, because Round uses Banker's rounding by default, that is, rounding six to five to get the pair. In fact, this is also the specification of IEEE, so all languages ​​that comply with the IEEE standard should use such algorithms.

The so-called even number means looking at the previous one: if it is an even number, the decimal number is 5 and discarded; if it is an odd number, the decimal number is 5 and 1 is entered. Therefore, if written: (74.5)=74;

(7.015, 2, ) = 7.02; // Keep multiple decimal places(-7.015, 2, ) = -7.02;

If multiple decimal places need to be retained, the enum type is also required.

// Round has eight overloads, and the output type is determined by the input typeRound(Decimal)
Round(Double)
Round(Decimal, Int32)
Round(Decimal, MidpointRounding)
Round(Double, Int32)
Round(Double, MidpointRounding)
Round(Decimal, Int32, MidpointRounding)
Round(Double, Int32, MidpointRounding)

aboutMidpointRoundingOfficial website link.

2. Further Dharma () and abandon Dharma ()

double[] values = { 7.03, 7.64, 0.12, -0.12, -7.1, -7.6 };
("Input Value Ceiling Results Floor Results\n");
foreach (double value in values)
    ("{0,7} {1,16} {2,14}", value, (value), (value));
// Enter value Ceiling result Floor result//   7.03                8              7
//   7.64                8              7
//   0.12                1              0
//  -0.12               -0             -1
//   -7.1               -7             -8
//   -7.6               -7             -8

2. Take a random number Random()

In the new version of C#, the Random class is included in the namespace System, so it can be directly instantiated and used anytime, anywhere, which is very convenient.

The following is a brief introduction to the usage of several overloads using examples, and more detailed usage recommendationsRandomOfficial documentation.

Random random = new Random();
// () // Return non-negative int random numberint randomvalue = (); // 0 <= Return integer with value <2,147,483,647// (Int) // Returns a non-negative random integer smaller than the specified maximum valueint randomvalue1 = (10); // 0 <= Return integer with value < 10// (Int,Int) // Return a random integer number in the specified range, for example (-100, 0) returns a negative numberint randomvalue2 = (10,20); // 10 <= Return integer with value < 20int randomvalue2 = (-10,0); // -10 <= Return integer with value < 0// () // Return a random number between 0 and 1 0.70865696329095262double randomvalue2 = (); // For example: 0.70865696329095262, 0.021905906508632892

3. Absolute value ()

The method of finding absolute values ​​is only overloaded with only one parameter, that is, the input parameters are different, but the output parameters and input parameters are the same, so I won't repeat it very simply.()Official Documentation

decimal[] decimals = { , 12.45M, 0M, -19.69M,
                       };
foreach (decimal value in decimals)
    ($"Abs({value}) = {(value)}");
//Abs(79228162514264337593543950335) = 79228162514264337593543950335
//Abs(12.45) = 12.45
//Abs(0) = 0
//Abs(-19.69) = 19.69
//Abs(-79228162514264337593543950335) = 79228162514264337593543950335

4. Take the larger value () and take the minimum value ()

The most commonly used way of writing is to compare the size of two numbers. They also support a variety of types, see()Official website.

(0,1) = 1;
(0,1) = 0;

Five, power () and square root ()

A simple example is as follows:

(2, 3) = 8; // 2 to the 3rd power(16) = 4; // 16 The square root of 4,Right now 4*4 = 16

This is the end of this article about the detailed explanation of commonly used mathematical operations in C# Math. For more related contents of commonly used mathematical operations in C# Math, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!