jQuery and ASP.NET

July 13, 2009

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

C#

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);
Console.ReadLine();
}
}

VB.NET

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)
Console.ReadLine()
End Sub
End Class

Output

image

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



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

1 Response to "Seperating the Integral and Fractional portion of a Decimal using C# or VB.NET"
  1. Anonymous said...
    February 5, 2010 5:23 AM

    Thanks a ton this is what was required

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions