
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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 −
Decimal.Add()
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); Decimal res = Decimal.Add(val1, 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); Decimal res = Decimal.Add(val1, 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
- Related Articles
- UInt16 Struct in C#
- UInt32 Struct in C#
- Char Struct in C#
- Byte Struct in C#
- UInt64 Struct in C#
- C# Int16 Struct
- C# Int32 Struct
- Int 64 Struct in C#
- Difference between 'struct' and 'typedef struct' in C++?
- C/C++ Struct vs Class
- Operations on struct variables in C
- Difference between 'struct' and 'typedef struct' in C++ program?
- Find max in struct array using C++.
- Deep Copy of Struct Member Arrays in C
- struct module in Python
