# Decimal

***

**1. Financial Calculations and Currency Conversion**

```c#
// Convert 100 USD to EUR at an exchange rate of 0.85
decimal usd = 100m;
decimal eurExchangeRate = 0.85m;
decimal eur = usd * eurExchangeRate;
```

**2. Scientific Calculations and Precision**

```c#
// Calculate the distance between two points (5, 5) and (10, 10)
decimal x1 = 5m;
decimal y1 = 5m;
decimal x2 = 10m;
decimal y2 = 10m;
decimal distance = Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
```

**3. Percentage Calculations**

```c#
// Calculate the percentage of 20 out of 100
decimal a = 20m;
decimal b = 100m;
decimal percentage = a / b * 100;
```

**4. Number Manipulation and Operations**

```c#
// Divide the decimal 10 by 3 and round the result to 2 decimal places
decimal value = 10m;
decimal result = Math.Round(value / 3, 2);
```

**5. Comparisons and Relational Operators**

```c#
// Check if decimal 1 is greater than decimal 0.5
decimal d1 = 1m;
decimal d2 = 0.5m;
bool isGreaterThan = d1 > d2;
```

**6. Casting and Type Conversion**

```c#
// Convert the double 1.5 to decimal
double d = 1.5;
decimal dec = (decimal)d;
```

**7. String Parsing and Conversion**

```c#
// Parse the string "3.14" to decimal
decimal parsedDecimal = decimal.Parse("3.14");
```

**8. Formatting and Display**

```c#
// Format the decimal 3.14 to the currency format "$3.14"
decimal value = 3.14m;
string formattedValue = value.ToString("C");
```

**9. Comparison of Decimals with Precision**

```c#
// Use decimal.Equals() to compare decimals with high precision
decimal d1 = 1.000000000000000000000000000000000000000000000000000000001m;
decimal d2 = 1.000000000000000000000000000000000000000000000000000000000m;
bool isEqual = decimal.Equals(d1, d2);
```

**10. Incrementing and Decrementing Decimals**

```c#
// Increment the decimal 1.0 by 0.1 using the ++ operator
decimal value = 1.0m;
value++;
```

**11. NaN and Infinity Handling**

```c#
// Check if a decimal is NaN (Not-a-Number)
decimal nan = decimal.NaN;
bool isNaN = decimal.IsNaN(nan);
```

**12. Min and Max Values**

```c#
// Get the minimum decimal value
decimal minValue = decimal.MinValue;
```

**13. Divide by Zero Handling**

```c#
try
{
    // Divide a decimal by 0
    decimal d1 = 10m;
    decimal d2 = 0m;
    decimal result = d1 / d2;
}
catch (DivideByZeroException)
{
    // Handle the divide by zero exception
}
```

**14. Floor and Ceiling Functions**

```c#
// Get the floor of a decimal (rounded down)
decimal value = 3.7m;
decimal floor = decimal.Floor(value);
```

**15. Decimal Rounding**

```c#
// Round the decimal 3.5678 to 2 decimal places
decimal value = 3.5678m;
decimal roundedValue = Math.Round(value, 2);
```

**16. Truncation**

```c#
// Truncate a decimal by removing the fractional part
decimal value = 3.789m;
decimal truncatedValue = Math.Truncate(value);
```

**17. Random Decimal Generation**

```c#
// Use Random to generate a random decimal between 0 and 1
Random random = new Random();
decimal randomNumber = (decimal)random.NextDouble();
```

**18. Nullable Decimals**

```c#
// Declare a nullable decimal variable
decimal? nullableValue = null;
```

**19. Scaling and Normalization**

```c#
// Scale a decimal by multiplying it by a power of 10
decimal value = 3.14m;
decimal scaledValue = value * 100m;
```

**20. Absolute Value**

```c#
// Get the absolute value of a decimal (remove the negative sign)
decimal value = -3.14m;
decimal absoluteValue = Math.Abs(value);
```

**21. Square Root**

```c#
// Calculate the square root of a decimal
decimal value = 16m;
decimal squareRoot = Math.Sqrt(value);
```

**22. Trigonometric Functions**

```c#
// Calculate the sine of a decimal angle (in radians)
decimal angle = (decimal)Math.PI / 2;
decimal sine = Math.Sin(angle);
```

**23. Exponents**

```c#
// Calculate 2 raised to the power of 10
decimal value = 2m;
decimal result = Math.Pow(value, 10);
```

**24. Logarithms**

```c#
// Calculate the base 10 logarithm of a decimal
decimal value = 100m;
decimal log10 = Math.Log10(value);
```

**25. Decimal Arrays and Collections**

```c#
// Create an array of decimals
decimal[] decimalArray = { 1.0m, 2.0m, 3.0m };
```

**26. Decimal Lists**

```c#
// Create a list of decimals
List<decimal> decimalList = new List<decimal> { 1.0m, 2.0m, 3.0m };
```

**27. Decimal Dictionaries**

```c#
// Create a dictionary with decimal keys and values
Dictionary<decimal, decimal> decimalDictionary = new Dictionary<decimal, decimal>();
decimalDictionary.Add(1.0m, 10.0m);
```

**28. Decimal IEnumerables**

```c#
// Create an IEnumerable of decimals
IEnumerable<decimal> decimalEnumerable = decimalArray.AsEnumerable();
```

**29. Decimal Ranges**

```c#
// Create a range of decimals from 1 to 10
var decimalRange = Enumerable.Range(1, 10).Select(n => (decimal)n);
```

**30. Decimal LINQ Queries**

```c#
// Select all decimals greater than 2 from an array
decimal[] decimalArray = { 1.0m, 2.0m, 3.0m };
var result = decimalArray.Where(d => d > 2.0m);
```

**31. Decimal Aggregations**

```c#
// Sum all decimals in an array
decimal[] decimalArray = { 1.0m, 2.0m, 3.0m };
decimal sum = decimalArray.Sum();
```

**32. Decimal Serialization and Deserialization**

```c#
// Serialize a decimal to JSON using Newtonsoft.Json
decimal value = 1.23m;
string json = JsonConvert.SerializeObject(value);

// Deserialize the decimal from JSON
decimal deserializedValue = JsonConvert.DeserializeObject<decimal>(json);
```

**33. Decimal with Attributes**

```c#
// Define a class with a decimal property decorated with attributes
public class MyClass
{
    [Range(0, 100)]
    public decimal MyDecimal { get; set; }
}
```

**34. Decimal with Data Annotations**

```c#
// Define a class with a decimal property decorated with data annotations
public class MyClass
{
    [Required]
    [Range(0, 100)]
    public decimal MyDecimal { get; set; }
}
```

**35. Decimal with Enum as Underlying Type**

```c#
// Define an enum with underlying type decimal
public enum MyEnum : decimal
{
    Value1 = 1.0m,
    Value2 = 2.0m,
    Value3 = 3.0m
}
```

**36. Decimal with Flags Attribute**

```c#
// Define an enum with underlying type decimal and decorate it with Flags attribute
[Flags]
public enum MyEnum : decimal
{
    Value1 = 1.0m,
    Value2 = 2.0m,
    Value3 = 4.0m
}
```

**37. Decimal with Serializable Attribute**

```c#
// Define a class with a decimal property decorated with Serializable attribute
[Serializable]
public class MyClass
{
    public decimal MyDecimal { get; set; }
}
```

**38. Decimal with NonSerialized Attribute**

```c#
// Define a class with a decimal property decorated with NonSerialized attribute
public class MyClass
{
    [NonSerialized]
    public decimal MyDecimal { get; set; }
}
```

**39. Decimal with Obsolete Attribute**

```c#
// Define a class with a decimal property decorated with Obsolete attribute
[Obsolete("Use MyNewDecimal property instead.")]
public decimal MyDecimal { get; set; }

public decimal MyNewDecimal { get; set; }
```

**40. Decimal with Conditional Attribute**

```c#
// Define a class with a decimal property decorated with Conditional attribute
public class MyClass
{
    [Conditional("DEBUG")]
    public decimal MyDecimal { get; set; }
}
```

**41. Decimal with Extension Methods**

```c#
// Define an extension method for decimal
public static decimal MultiplyByTwo(this decimal value)
{
    return value * 2;
}
```

**42. Decimal with Static Constructors**

```c#
// Define a class with a static constructor that initializes a decimal field
public class MyClass
{
    public static decimal MyDecimal;

    static MyClass()
    {
        MyDecimal = 1.0m;
    }
}
```

**43. Decimal with Finalizers**

```c#
// Define a class with a decimal field and a finalizer
public class MyClass
{
    public decimal MyDecimal;

    ~MyClass()
    {
        // Cleanup resources associated with MyDecimal
    }
}
```

**44. Decimal with Indexers**

```c#
// Define a class with an indexer that returns a decimal
public class MyClass
{
    private decimal[] _values;

    public MyClass(decimal[] values)
    {
        _values = values;
    }

    public decimal this[int index]
    {
        get { return _values[index]; }
        set { _values[index] = value; }
    }
}
```

**45. Decimal with Events**

```c#
// Define a class with an event that passes a decimal argument
public class MyClass
{
    public delegate void MyEventHandler(decimal value);

    public event MyEventHandler MyEvent;

    public void TriggerEvent(decimal value)
    {
        // Invoke the event handler
        MyEvent?.Invoke(value);
    }
}
```

**46. Decimal with Delegates**

```c#
// Define a delegate that takes a decimal argument
public delegate void MyDelegate(decimal value);
```

**47. Decimal with Anonymous Types**

```c#
// Create an anonymous type with a decimal property
var myObject = new { MyDecimal = 1.0m };
```

**48. Decimal with Tuples**

```c#
// Create a tuple with a decimal element
var myTuple = (1.0m, "hello");
```

**49. Decimal with Nullable Value Types**

```c#
// Declare a nullable decimal variable
decimal? nullableDecimal = null;
```

**50. Decimal with Generics**

```c#
// Define a generic class with a decimal field
public class MyGenericClass<T>
{
    public T MyValue;
}

var decimalClass = new MyGenericClass<decimal> { MyValue = 1.0m };
```
