# Decimal Struct in C#

The Decimal Struct in C# Represents a decimal floating-point number. The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The default value of a Decimal is 0.

Let us now see some examples of the methods in Decimal Struct −

The Decimal.Add() method in C# is used to add two specified Decimal values.

### Syntax

Following is the syntax −

public static decimal Add (decimal val1, decimal val2);

Above, va1 is the first decimal to add, whereas val2 is the second decimal to be added.

### Example

Let us now see an example to implement the Decimal.Add() method −

using System;
public class Demo {
public static void Main(){
Decimal val1 = 3.07m;
Decimal val2 = 4.09m;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Result (Sum) = "+res);
}
}

### Output

This will produce the following output −

Decimal 1 = 3.07
Decimal 2 = 4.09
Result (Sum) = 7.16

### Example

Let us now see another example to implement the Decimal.Add() method −

using System;
public class Demo {
public static void Main(){
Decimal val1 = Decimal.MinValue;
Decimal val2 = 8.21m;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Result (Sum) = "+res);
}
}

### Output

This will produce the following output −

Decimal 1 = -79228162514264337593543950335
Decimal 2 = 8.21
Result (Sum) = -79228162514264337593543950327

## Decimal.Ceiling()

The Decimal.Ceiling() method in C# is used to return the smallest integral value greater than or equal to the specified decimal number.

### Syntax

Following is the syntax −

public static decimal Ceiling (decimal val);

Above, Val is the decimal number.

### Example

Let us now see an example to implement the Decimal.Ceiling() method −

using System;
public class Demo {
public static void Main(){
Decimal val1 = 12.85m;
Decimal val2 = 3.45m;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Ceiling (val1) = "+Decimal.Ceiling(val1));
Console.WriteLine("Ceiling (val2) = "+Decimal.Ceiling(val2));
}
}

### Output

This will produce the following output −

Decimal 1 = 12.85
Decimal 2 = 3.45
Ceiling (val1) = 13
Ceiling (val2) = 4

### Example

Let us now see another example to implement the Decimal.Ceiling() method −

using System;
public class Demo {
public static void Main(){
Decimal val1 = -10.85m;
Decimal val2 = -33.45m;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Ceiling (val1) = "+Decimal.Ceiling(val1));
Console.WriteLine("Ceiling (val2) = "+Decimal.Ceiling(val2));
}
}

### Output

This will produce the following output −

Decimal 1 = -10.85
Decimal 2 = -33.45
Ceiling (val1) = -10
Ceiling (val2) = -33

## Decimal.Compare()

The Decimal.Compare() method in C# is used to compare two specified Decimal values.

### Syntax

Following is the syntax −

public static int Compare (decimal val1, decimal val2);

Above, val1 is the first value to compare, whereas Val is the second value to compare.

The return value is less than zero if val1 is less than val2. Return value is 0, if Val = val2, whereas greater than zero, if val1 is greater than val2.

### Example

Let us now see an example to implement the Decimal.Compare() method −

using System;
public class Demo {
public static void Main(){
Decimal val1 = 45.85m;
Decimal val2 = 35.45m;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Comparison Value = "+Decimal.Compare(val1,val2));
}
}

### Output

This will produce the following output −

Decimal 1 = 45.85
Decimal 2 = 35.45
Comparison Value = 1

### Example

Let us now see another example to implement the Decimal.Compare() method −

using System;
public class Demo {
public static void Main(){
Decimal val1 = 65.15m;
Decimal val2 = 65.15m;
Console.WriteLine("Decimal 1 = "+val1);
Console.WriteLine("Decimal 2 = "+val2);
Console.WriteLine("Comparison Value = "+Decimal.Compare(val1,val2));
}
}

### Output

This will produce the following output −

Decimal 1 = 65.15
Decimal 2 = 65.15
Comparison Value = 0

Updated on: 14-Nov-2019

301 Views