# Decimal in C#: Various Methods to Work with Decimal Data Types

C# is a simple yet powerful object oriented language which gives programmers the ability to build a host of different applications. It resembles C++ and is the part of the .Net Framework. This language is specifically defined for common language infrastructure (CLI) which comprises executable code and runtime environment which permits the use of various high-level languages across diverse computer platforms and architecture. Moreover this language is easy to learn and produces highly efficient programs. Today we walk you through this intermediate level tutorial on Decimal data type. This requires a basic comfort level with C# and .NET programming. If these are new to you, you can first take up this beginners course on C# and .NET.

## What is Decimal Data Type?

Decimal data type is used to accurately store numeric data. This type is needed in programs where rounding errors can cause problematic loss of accuracy. This is a value type which can be manipulated with the standard numerical operators in the C# programming language. The operators include those for addition, subtraction, multiplication and division. Also you can test decimal values with the use of the equality operator. Lets go through a simple C# program which makes use of decimals.

**Example 1: Program to Display the Arithmetic Operations**

Using System;

class Example

{

static void Main()

{

decimal y = 1.0M;

if (y == 1.0M)

{

Console.WriteLine(y);

}

decimal x = 0.5M;

x += y;

Console.WriteLine(y);

x -= (y * 2);

Console.WriteLine(x);

}

}

In this program,’ y’ is declared as of type decimal and assigned a decimal value. The if statement checks whether the value stored in ‘y’ is of decimal data type. In that case, print the value of y on the screen. Then we declare another variable ‘x’ of type decimal and assign it a decimal point. We perform different arithmetic operations combining ‘x’ and ‘y’. You will see that the result of any arithmetic operations on the two decimal types will result in only a decimal value. To learn more about writing your own programs in C#, you can take this beginners course.

**Example 2: **Constant Method

In order to get the maximum and minimum values of the decimal type, you can access the MaxValue and MinValue constants. Other constants such as the One, Zero, and MinusOne can also be used.

using System; class Example { static void Main() { Console.WriteLine(decimal.MaxValue); Console.WriteLine(decimal.MinValue); Console.WriteLine(decimal.One); Console.WriteLine(decimal.Zero); Console.WriteLine(decimal.MinusOne); } }

The above program will output the maximum value possible for the decimal data type, the minimum value possible, 1, 0 and -1. This course can help you learn more about data types and constant methods.

**Example 3:** Floor and Ceiling Methods

Ceiling is the integer immediately more than the decimal value and floor is the first integer lower than the decimal value. For example when you take the ceiling of 2.3, we get three, when you take the floor of the same number you get 2.

using System; class Example { static void Main() { Console.WriteLine(decimal.Ceiling(2.7M)); Console.WriteLine(decimal.Floor(2.7M)); } }

In this program, the output is 3 and 2. 3 is the integer immediately above 2.7 decimal value. Also 2 is the integer immediately below 2.7 decimal value.

**Example 4: **Remainder Method

When you do a division operation, the remainder is the part left over.

using System; class Example { static void Main() { Console.WriteLine(decimal.Remainder(2.0M, 1.7M)); } }

In this program, we get the remainder of 2.0m divided by 1.7m. The output is 0.3

## Example 5: Truncate Method

This method erases all the numbers which occur after the decimal place. For example in the following program when we truncate 7.86, we get the number 7. Truncate will always return a value of type decimal.

using System; class Example { static void Main() { Console.WriteLine(decimal.Truncate(8.99M)); } }

In the above program, Truncate method removes all the integers after the decimal place. The output of the program is 8. To explore more about the various methods associated with data types, take this course.

## Example 6: Negate Method

The negate method makes positive decimals negative and vice versa.

using System; class Example { static void Main() { Console.WriteLine(decimal.Negate(3.2M)); Console.WriteLine(decimal.Negate(-3.2M)); } }

In this program, the positive decimal type value is turned into the negative value and vice versa.

## Example 7: Arithmetic Method

In order to do decimal manipulations, there are the Add, Multiply, Subtract and Divide methods. Each method accepts two values at a time.

using System; class Example { static void Main() { Console.WriteLine(decimal.Add(3.1M, 5.3M)); Console.WriteLine(decimal.Multiply(8.0M, 3.0M)); Console.WriteLine(decimal.Subtract(5.7M, 1.3M)); Console.WriteLine(decimal.Divide(12M, 3M)); } }

The output for the above methods is as follows.

8.4 24.0 4.4 4

## Example 8: Round, Midpoint Rounding Methods

The Round method by default rounds down or up to the specified decimal place. In order to further refine the required behavior, you have to specify the midpoint rounding method.

When you use ToEven method, the number is rounded to the nearest even number. Also we have the AwayFromZero method in which numbers are rounded up for positive numbers and down for negative numbers.

using System; class Example { static void Main() { Console.WriteLine(decimal.Round(2.39M, 1)); Console.WriteLine(decimal.Round(3.5M, 0, MidpointRounding.ToEven)); Console.WriteLine(decimal.Round(3.5M, 0, MidpointRounding.AwayFromZero)); } }

In this program, the decimal value 2.39 is rounded to 2.4. 3.5 is rounded to the nearest even number which is 4. 3.5 is rounded to the 4 which is nearest number and away from zero.

## Example 9: Parse, TryParse Methods

The parse method of decimal type converts strings to actual decimals. This method will throw an exception if it contains an incorrectly formatted value. Errors are handled by decimal.TryParse method which returns a false value. As a rule, you should use Parse when you expect correct input. On the other hand if you expect a lot of errors use the TryParse method.

using System; class Example { static void Main() { decimal value = decimal.Parse("100.01"); Console.WriteLine(value); decimal value2; if (decimal.TryParse("xyz", out value2)) { Console.WriteLine("Not reached"); } } }

In the program, the string 100.01 is converted to the decimal value 100.01. This is displayed using the WriteLine method of Console.

## Example 10: Convert method

The decimal type is of type 16 bytes. Hence, it’s a waste of memory to store large number of decimals when you do not need that much accuracy. There exist methods to convert to smaller memory representations of the relevant number.

using System; class Example { static void Main() { const decimal input = 9; Console.WriteLine(decimal.ToByte(input)); Console.WriteLine(decimal.ToDouble(input)); Console.WriteLine(decimal.ToInt16(input)); Console.WriteLine(decimal.ToInt32(input)); Console.WriteLine(decimal.ToInt64(input)); Console.WriteLine(decimal.ToSByte(input)); Console.WriteLine(decimal.ToSingle(input)); Console.WriteLine(decimal.ToUInt16(input)); Console.WriteLine(decimal.ToUInt32(input)); Console.WriteLine(decimal.ToUInt64(input)); } }

All the converting functions will contain the decimal 9 but have different amounts of storage space. You can learn more about these advanced Decimal methods in this C# course.

## Decimal memory Usage

C# Decimal type values requires 16 bits of storage space. Note that it requires more memory than most other value types commonly used in the C# programming language. The decimal type is usually used in cases which require high accuracy of values and have many large numbers. Hence, currencies and monetary values are usually used with the decimal type. As the decimal type is 16 bytes, it will be less efficient than the native integer in low level code. In spite of this, when the accuracy of monetary sums is at risk , reduced performance is preferable.

The decimal type is a powerful and accurate tool to represent number data in memory. It contains a host of static methods and hence is immensely useful for representing important and significant quantities such as large amounts of money.

Hope this article helped you understand the decimal type in the C# programming language. The best way to master any programming language, is to write your own programs. Who knows, you may be soon writing the next killer Android app in C# (you can this course to help you along)!

## Recommended Articles

### Top courses in C#

### C# students also learn

## Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.