C# Number Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
In C#, you can convert decimal values to double values using explicit casting or the Convert
method. Here's how to do it:
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
.
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[]
.
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[][]
.
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.
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'.
Working with integers in C#
int a = 5; int b = 3; int sum = a + b;
C# floating-point numbers
float floatValue = 3.14f; double doubleValue = 3.14159265359;
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)
C# mathematical functions
C# provides various mathematical functions in the Math
class:
double squareRoot = Math.Sqrt(25); double power = Math.Pow(2, 3);
Parsing and formatting numbers in C#
string numberString = "42"; int parsedNumber = int.Parse(numberString); int number = 42; string formattedNumber = number.ToString();
C# random number generation
Random random = new Random(); int randomNumber = random.Next(1, 101); // Generates a random number between 1 and 100
Working with decimals in C#
decimal decimalNumber = 3.14m;
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
C# rounding and precision
double value = 3.14159; double roundedValue = Math.Round(value, 2); // Rounds to 2 decimal places
Number formatting in C#
int number = 1234567; string formattedNumber = number.ToString("N"); // Adds commas: 1,234,567
C# converting between numeric types
int intValue = 42; double doubleValue = Convert.ToDouble(intValue);
BigInteger in C# for large numbers
BigInteger bigIntegerValue = BigInteger.Parse("1234567890123456789012345678901234567890");
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
Number comparison in C#
int a = 5; int b = 3; bool isEqual = a == b; bool isNotEqual = a != b;
C# checking if a number is even or odd
int number = 42; bool isEven = (number % 2 == 0); bool isOdd = (number % 2 != 0);
Hexadecimal and binary representation of numbers in C#
int hexValue = 0x1A; // Hexadecimal int binaryValue = 0b1010; // Binary
C# trigonometric functions
double angleInRadians = Math.PI / 4; double sineValue = Math.Sin(angleInRadians); double cosineValue = Math.Cos(angleInRadians);
Number manipulation libraries in C#
Libraries like System.Numerics
provide advanced numeric types and operations.