How to round to X decimal places in C# and VB.NET

To round to X decimal places in C# and VB.NET you can use one of the following methods.
Take a look at the output image to see the results.

Sample ConsoleApp in C#

static void Main(string[] args)
{
	decimal input = Decimal.Parse("3,546932");
	decimal inputZero = Decimal.Zero;
	decimal inputShort = Decimal.Parse("3,545");
	decimal inputShorter = 1;

	Console.WriteLine("######################################################################");
	Console.WriteLine("");
	Console.WriteLine("Testinput1:\t{0}", input);
	Console.WriteLine("Testinput2:\t{0}", inputZero);
	Console.WriteLine("Testinput3:\t{0}", inputShort);
	Console.WriteLine("Testinput4:\t{0}", inputShorter);
	Console.WriteLine("");

	Console.WriteLine("########## using decimal.Round AwayFromZero 2 Decimals ##########");
	Console.WriteLine("");
	Console.WriteLine("Output 1:\t{0}",decimal.Round(input, 2, MidpointRounding.AwayFromZero));
	Console.WriteLine("Output 2:\t{0}", decimal.Round(inputShort, 2, MidpointRounding.AwayFromZero));
	Console.WriteLine("Output 3:\t{0}", decimal.Round(inputShorter, 2, MidpointRounding.AwayFromZero));
	Console.WriteLine("Output 4:\t{0}", decimal.Round(inputZero, 2, MidpointRounding.AwayFromZero));
	Console.WriteLine("");

	Console.WriteLine("########## using decimal.Round ToEven 2 Decimals ##########");
	Console.WriteLine("");
	Console.WriteLine("Output 1:\t{0}", decimal.Round(input, 2, MidpointRounding.ToEven));
	Console.WriteLine("Output 2:\t{0}", decimal.Round(inputShort, 2, MidpointRounding.ToEven));
	Console.WriteLine("Output 3:\t{0}", decimal.Round(inputShorter, 2, MidpointRounding.ToEven));
	Console.WriteLine("Output 4:\t{0}", decimal.Round(inputZero, 2, MidpointRounding.ToEven));
	Console.WriteLine("");

	Console.WriteLine("########## using .ToString(\"0.00\") 2 Decimals ##########");
	Console.WriteLine("");
	Console.WriteLine("Output 1:\t{0}", input.ToString("0.00"));
	Console.WriteLine("Output 2:\t{0}", inputShort.ToString("0.00"));
	Console.WriteLine("Output 3:\t{0}", inputShorter.ToString("0.00"));
	Console.WriteLine("Output 4:\t{0}", inputZero.ToString("0.00"));
	Console.WriteLine("");

	Console.WriteLine("########## using .ToString(\"0.00\") 5 Decimals ##########");
	Console.WriteLine("");
	Console.WriteLine("Output 1:\t{0}", input.ToString("0.00000"));
	Console.WriteLine("Output 2:\t{0}", inputShort.ToString("0.00000"));
	Console.WriteLine("Output 3:\t{0}", inputShorter.ToString("0.00000"));
	Console.WriteLine("Output 4:\t{0}", inputZero.ToString("0.00000"));
	Console.WriteLine("");

	Console.Read();
}

Sample ConsoleApp in VB.NET

Private Shared Sub Main(args As String())
	Dim input As Decimal = [Decimal].Parse("3,546932")
	Dim inputZero As Decimal = [Decimal].Zero
	Dim inputShort As Decimal = [Decimal].Parse("3,545")
	Dim inputShorter As Decimal = 1

	Console.WriteLine("######################################################################")
	Console.WriteLine("")
	Console.WriteLine("Testinput1:" & vbTab & "{0}", input)
	Console.WriteLine("Testinput2:" & vbTab & "{0}", inputZero)
	Console.WriteLine("Testinput3:" & vbTab & "{0}", inputShort)
	Console.WriteLine("Testinput4:" & vbTab & "{0}", inputShorter)
	Console.WriteLine("")

	Console.WriteLine("########## using decimal.Round AwayFromZero 2 Decimals ##########")
	Console.WriteLine("")
	Console.WriteLine("Output 1:" & vbTab & "{0}", Decimal.Round(input, 2, MidpointRounding.AwayFromZero))
	Console.WriteLine("Output 2:" & vbTab & "{0}", Decimal.Round(inputShort, 2, MidpointRounding.AwayFromZero))
	Console.WriteLine("Output 3:" & vbTab & "{0}", Decimal.Round(inputShorter, 2, MidpointRounding.AwayFromZero))
	Console.WriteLine("Output 4:" & vbTab & "{0}", Decimal.Round(inputZero, 2, MidpointRounding.AwayFromZero))
	Console.WriteLine("")

	Console.WriteLine("########## using decimal.Round ToEven 2 Decimals ##########")
	Console.WriteLine("")
	Console.WriteLine("Output 1:" & vbTab & "{0}", Decimal.Round(input, 2, MidpointRounding.ToEven))
	Console.WriteLine("Output 2:" & vbTab & "{0}", Decimal.Round(inputShort, 2, MidpointRounding.ToEven))
	Console.WriteLine("Output 3:" & vbTab & "{0}", Decimal.Round(inputShorter, 2, MidpointRounding.ToEven))
	Console.WriteLine("Output 4:" & vbTab & "{0}", Decimal.Round(inputZero, 2, MidpointRounding.ToEven))
	Console.WriteLine("")

	Console.WriteLine("########## using .ToString(""0.00"") 2 Decimals ##########")
	Console.WriteLine("")
	Console.WriteLine("Output 1:" & vbTab & "{0}", input.ToString("0.00"))
	Console.WriteLine("Output 2:" & vbTab & "{0}", inputShort.ToString("0.00"))
	Console.WriteLine("Output 3:" & vbTab & "{0}", inputShorter.ToString("0.00"))
	Console.WriteLine("Output 4:" & vbTab & "{0}", inputZero.ToString("0.00"))
	Console.WriteLine("")

	Console.WriteLine("########## using .ToString(""0.00"") 5 Decimals ##########")
	Console.WriteLine("")
	Console.WriteLine("Output 1:" & vbTab & "{0}", input.ToString("0.00000"))
	Console.WriteLine("Output 2:" & vbTab & "{0}", inputShort.ToString("0.00000"))
	Console.WriteLine("Output 3:" & vbTab & "{0}", inputShorter.ToString("0.00000"))
	Console.WriteLine("Output 4:" & vbTab & "{0}", inputZero.ToString("0.00000"))
	Console.WriteLine("")

	Console.Read()
End Sub

OUTPUT

Decimal Place Formatting

Decimal Place Formatting

Please Share us:
Please rate this snippet

2 thoughts on “How to round to X decimal places in C# and VB.NET

Leave a Reply