C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Also: You can test decimal values with the equality operator == and print decimals to the Console.
ConsoleC# program that uses decimal operators
using System;
class Program
{
static void Main()
{
decimal d = 1.0M;
// Test decimal.
if (d == 1.0M)
{
Console.WriteLine(d);
}
decimal y = 0.5M;
// Add decimal.
y += d;
Console.WriteLine(y);
// Subtract and multiply decimal.
y -= (d * 2);
Console.WriteLine(y);
}
}
Output
1.0
1.5
-0.5
C# program that reveals decimal constants
using System;
class Program
{
static void Main()
{
Console.WriteLine(decimal.MaxValue);
Console.WriteLine(decimal.MinValue);
Console.WriteLine(decimal.One);
Console.WriteLine(decimal.Zero);
Console.WriteLine(decimal.MinusOne);
}
}
Output
79228162514264337593543950335
-79228162514264337593543950335
1
0
-1
Info: When you take the ceiling of 1.1, you get 2. When you take the floor of that same number, you receive 1.
Math.CeilingMath.FloorSo: Ceiling is the integer above the number and the floor is the integer below the number.
C# program that uses Ceiling and Floor methods
using System;
class Program
{
static void Main()
{
Console.WriteLine(decimal.Ceiling(1.1M));
Console.WriteLine(decimal.Floor(1.1M));
}
}
Output
2
1
C# program that uses math methods
using System;
class Program
{
static void Main()
{
Console.WriteLine(decimal.Add(1.1M, 1.3M)); // Add
Console.WriteLine(decimal.Multiply(5.0M, 2.0M)); // Five times two
Console.WriteLine(decimal.Subtract(2.0M, 1.0M)); // Two minus one
Console.WriteLine(decimal.Divide(10M, 2M)); // Ten divided by two
}
}
Output
2.4
10.00
1.0
5
Here: We use the Remainder() method. We show that the remainder of 2 divided by 1.5 is 0.5.
Tip: This is because the 0.5 is left over after the division occurred. This is called the remainder.
C# program that uses Remainder method
using System;
class Program
{
static void Main()
{
// Remainder of 2 divided by 1.5 is 0.5
Console.WriteLine(decimal.Remainder(2.0M, 1.5M));
}
}
Output
0.5
Here: This program shows that when we truncate 4.55, we get the number 4. Truncate returns a value of type decimal.
Info: Truncate is similar to casting down, as with (int), but has different behavior on values that cannot be represented with int.
Int, uintCast, IntC# program that uses Truncate method
using System;
class Program
{
static void Main()
{
// Truncate removes the digits after the decimal place.
Console.WriteLine(decimal.Truncate(4.55M));
}
}
Output
4
C# program that uses Negate method
using System;
class Program
{
static void Main()
{
// Makes positive numbers negative, and negative numbers positive.
Console.WriteLine(decimal.Negate(1.5M));
Console.WriteLine(decimal.Negate(-1.5M));
}
}
Output
-1.5
1.5
Note: The ToEven value means that when a number needs to be rounded, it is always rounded to the nearest even number.
Note 2: The AwayFromZero value means that numbers are rounded up for positive numbers and down for negative numbers.
C# program that uses decimal.Round
using System;
class Program
{
static void Main()
{
// Round to one decimal place.
Console.WriteLine(decimal.Round(1.59M, 1));
// Demonstrate MidpointRounding enumeration.
Console.WriteLine(decimal.Round(2.5M, 0, MidpointRounding.ToEven));
Console.WriteLine(decimal.Round(2.5M, 0, MidpointRounding.AwayFromZero));
}
}
Output
1.6
2
3
Tip: You should use Parse if you expect all input will be correct, and TryParse if you expect errors to be common.
int.ParseC# program that parses strings into decimals
using System;
class Program
{
static void Main()
{
// Use Parse on arguments that are always correct.
decimal value = decimal.Parse("100.01");
Console.WriteLine(value);
// Use TryParse when errors are expected.
decimal value2;
if (decimal.TryParse("perl", out value2))
{
Console.WriteLine("Not reached");
}
}
}
Output
100.01
Caution: Exceptions occur if the conversion cannot be done because the decimal value is too large to be represented in the target type.
C# program that converts from decimal type
using System;
class Program
{
static void Main()
{
const decimal input = 5;
// Convert this decimal into other compatible types.
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));
}
}
Output
5
5
5
5
5
5
5
5
5
5
Program: We show the byte size of the decimal type is equal to 16 bytes. The program allocates an array of one million decimal elements.
Info: The GC.GetTotalMemory method is used to acquire an estimate of the number of bytes allocated in the managed heap.
C# program that measures array of decimals
using System;
class Program
{
const int _count = 1000000; // One million decimals
static void Main()
{
long bytes1 = GC.GetTotalMemory(true);
//
// Allocate an array of decimal values.
// ... Assign an element to make the allocation not optimized out.
//
decimal[] array = new decimal[_count];
long bytes2 = GC.GetTotalMemory(true);
array[1] = 0.1M;
//
// Compute the memory usage of the decimal type.
//
long difference = bytes2 - bytes1;
double per = (double)difference / _count;
Console.WriteLine("Program used: {0:0.0} MB",
(double)difference / (1024 * 1024));
Console.WriteLine("Each decimal element required: {0:0.0} bytes",
per);
}
}
Output
Program used: 15.3 MB
Each decimal element required: 16.0 bytes
And: ToOACurrency takes a decimal and converts it into an OA value. OA values are a Microsoft-specific encoding.
FromOADateNote: You should only use these methods if you are translating Excel spreadsheets or certain Microsoft database formats.