Truncating Numbers In C#
[C#, .NET]
If you have the following number, 123.456
, and you want to write it to the console, or anywhere for that matter to 2 decimal places, you would do it like so:
Console.WriteLine(123.456.ToString("#,0.00"));
This would print the following:
123.46
What has happened here is that C# has rounded the value for you to 2 decimal places.
Suppose you don’t want this and actually want to truncate the number to 2 decimal places.
There is a Math.Truncate method, but it doesn’t do what you might think it does - it actually removes all the decimal values and returns the integral part.
In other words, 70.343
-> 70
However, you can achieve the result of truncating decimal places by using the Math.Round method and passing telling it to round towards zero using the MidpointRounding.ToZero
enum, like this:
Console.WriteLine(Math.Round(123.456,2,MidpointRounding.ToZero).ToString());
This would print the following
123.45
As usual, tests must be written to verify any assertions.
[Theory]
[InlineData(123.449, 123.44)]
[InlineData(123.450, 123.45)]
[InlineData(123.451, 123.45)]
[InlineData(123.454, 123.45)]
[InlineData(123.455, 123.45)]
[InlineData(123.456, 123.45)]
[InlineData(123.459, 123.45)]
[InlineData(123.460, 123.46)]
public void TruncationIsDoneCorrectly(decimal input, decimal expected)
{
Math.Round(input, 2, MidpointRounding.ToZero).Should().Be(expected);
}
You can view and run the code for the tests in my Github
Happy hacking!