This snippet will give you the PercentOf method for C# and VB.NET.

Sample C#

private decimal CalculatePercentOf(object value1, object value2)
{
	decimal val1;
	decimal val2;
	decimal.TryParse(value1.ToString(), out val1);
	decimal.TryParse(value2.ToString(), out val2);
	decimal result = 0;
	if (val1 > 0 && val2 > 0)
	{
		result = (decimal) val1/val2*100;
	}
	return result;
}

public static decimal PercentOf(sbyte first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, float second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, sbyte second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, short second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, int second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, long second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, double second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, decimal second)
{
	return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, float second)
{
	return CalculatePercentOf(first, second);
}

Sample VB.NET

Private Function CalculatePercentOf(value1 As Object, value2 As Object) As Decimal
	Dim val1 As Decimal
	Dim val2 As Decimal
	Decimal.TryParse(value1.ToString(), val1)
	Decimal.TryParse(value2.ToString(), val2)
	Dim result As Decimal = 0
	If val1 > 0 AndAlso val2 > 0 Then
		result = CDec(val1) / val2 * 100
	End If
	Return result
End Function

Public Shared Function PercentOf(first As SByte, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As SByte) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Short) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Integer) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Long) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Double) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Decimal) As Decimal
	Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Single) As Decimal
	Return CalculatePercentOf(first, second)
End Function

One thought on “PercentOf method for C# and VB.NET”

Leave a Reply