Seperating the Integral and Fractional portion of a Decimal using C# or VB.NET

In one of my recent visits to a client, I saw developers working on a functionality where they were separating the Integer and fractional portions of a Decimal. The code used by them was more than it was actually required.

Keeping note of the handy functions in the .NET framework can be extremely useful in such situations and can save you a lot of efforts. One similar function is the Math.Truncate() function. Here’s how to achieve the requirement using this function


class Program
static void Main(string[] args)
decimal num = 55.32m;
decimal integral = Math.Truncate(num);
decimal fractional = num - integral;
Console.WriteLine("Integral Part is {0} and Fractional Part is {1}",
integral, fractional);


Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim num As Decimal = 55.32D
Dim integral As Decimal = Math.Truncate(num)
Dim fractional As Decimal = num - integral
Console.WriteLine("Integral Part is {0} and Fractional Part is {1}", _
integral, fractional)
End Sub
End Class



All you need to do is now create a seperate function and add this code over there.

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Anonymous said...

Thanks a ton this is what was required