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

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**

## 1 comment:

Thanks a ton this is what was required

Post a Comment