Numbers in C#

In C#, you can convert decimal values to double values using explicit casting or the Convert method. Here's how to do it:

Converting Decimal to Double

decimal decimalValue = 10.5m;
double doubleValue1 = (double)decimalValue;
double doubleValue2 = Convert.ToDouble(decimalValue);

In this example, the decimalValue variable is explicitly cast to a double using the (double) syntax. Alternatively, the Convert.ToDouble method is called with the decimalValue variable as the argument to convert it to a double.

Converting Decimal Array to Double Array

decimal[] decimalArray = { 1.1m, 2.2m, 3.3m };
double[] doubleArray = decimalArray.Select(x => (double)x).ToArray();

In this example, the decimalArray variable is converted to a double array using LINQ's Select method to perform an explicit cast on each element, and then ToArray is called to convert the resulting IEnumerable<double> to a double[].

Converting decimal[][] to double[][]

decimal[][] decimalArray2D = { new decimal[] { 1.1m, 2.2m }, new decimal[] { 3.3m, 4.4m } };
double[][] doubleArray2D = decimalArray2D.Select(x => x.Select(y => (double)y).ToArray()).ToArray();

In this example, the decimalArray2D variable is converted to a double array using LINQ's Select method to perform an explicit cast on each element of the inner arrays, and then ToArray is called to convert the resulting IEnumerable<double> to a double[]. Finally, another ToArray is called on the resulting IEnumerable<double[]> to convert it to a double[][].

Rounding a Number to Two Decimal Places

double value = 10.56789;
double roundedValue = Math.Round(value, 2);

In this example, the Math.Round method is called with the value variable as the first argument, and 2 as the second argument to round the value to two decimal places.

Converting Char to Int

char charValue = '5';
int intValue = (int)charValue - (int)'0';

In this example, the charValue variable is explicitly cast to an int, and then the integer value of the character '0' is subtracted to convert the resulting value to an integer. Note that this method assumes that the charValue variable contains a single digit character between '0' and '9'.

  1. Working with integers in C#

    int a = 5;
    int b = 3;
    int sum = a + b;
    
  2. C# floating-point numbers

    float floatValue = 3.14f;
    double doubleValue = 3.14159265359;
    
  3. Arithmetic operations in C#

    int result = 5 + 3;  // Addition
    int difference = 5 - 3;  // Subtraction
    int product = 5 * 3;  // Multiplication
    int quotient = 5 / 3;  // Division (integer division)
    int remainder = 5 % 3;  // Modulo (remainder)
    
  4. C# mathematical functions

    C# provides various mathematical functions in the Math class:

    double squareRoot = Math.Sqrt(25);
    double power = Math.Pow(2, 3);
    
  5. Parsing and formatting numbers in C#

    string numberString = "42";
    int parsedNumber = int.Parse(numberString);
    
    int number = 42;
    string formattedNumber = number.ToString();
    
  6. C# random number generation

    Random random = new Random();
    int randomNumber = random.Next(1, 101);  // Generates a random number between 1 and 100
    
  7. Working with decimals in C#

    decimal decimalNumber = 3.14m;
    
  8. C# bitwise operations on numbers

    int x = 5;  // 0101 in binary
    int y = 3;  // 0011 in binary
    
    int resultAnd = x & y;  // Bitwise AND: 0001
    int resultOr = x | y;   // Bitwise OR: 0111
    int resultXor = x ^ y;  // Bitwise XOR: 0110
    
  9. C# rounding and precision

    double value = 3.14159;
    double roundedValue = Math.Round(value, 2);  // Rounds to 2 decimal places
    
  10. Number formatting in C#

    int number = 1234567;
    string formattedNumber = number.ToString("N");  // Adds commas: 1,234,567
    
  11. C# converting between numeric types

    int intValue = 42;
    double doubleValue = Convert.ToDouble(intValue);
    
  12. BigInteger in C# for large numbers

    BigInteger bigIntegerValue = BigInteger.Parse("1234567890123456789012345678901234567890");
    
  13. C# NaN and Infinity handling

    double result = 0.0 / 0.0;  // NaN
    double positiveInfinity = 1.0 / 0.0;  // Positive Infinity
    double negativeInfinity = -1.0 / 0.0;  // Negative Infinity
    
  14. Number comparison in C#

    int a = 5;
    int b = 3;
    
    bool isEqual = a == b;
    bool isNotEqual = a != b;
    
  15. C# checking if a number is even or odd

    int number = 42;
    
    bool isEven = (number % 2 == 0);
    bool isOdd = (number % 2 != 0);
    
  16. Hexadecimal and binary representation of numbers in C#

    int hexValue = 0x1A;  // Hexadecimal
    int binaryValue = 0b1010;  // Binary
    
  17. C# trigonometric functions

    double angleInRadians = Math.PI / 4;
    double sineValue = Math.Sin(angleInRadians);
    double cosineValue = Math.Cos(angleInRadians);
    
  18. Number manipulation libraries in C#

    Libraries like System.Numerics provide advanced numeric types and operations.