Trying to create a 50%/50% payment schedule in a report. The issue is when the number doesn’t split clean in half. For example, if I have $1800.01, I want to show first half $900.00 and second half $900.01. SSRS seems to want to lose the extra penny and give me $900.00 twice.
Public Function SplitValue(ByVal value As Decimal, ByVal isFirstHalf As Boolean) As Decimal
Dim halfValue As Decimal = value / 2
Dim roundedHalf As Decimal = Math.Round(halfValue, 2)
If isFirstHalf Then
Return roundedHalf
Else
Return value - roundedHalf
End If
End Function
I understand that you need to perform this splitting within the SSRS report itself. Given the complexities of SSRS rounding behavior, achieving this within the report can be challenging. However, I can suggest another approach that might work for your specific case.
You can use a combination of custom code and calculated fields to split the values while avoiding rounding issues. Here’s a step-by-step guide:
Custom Code:
Go to the “Report” menu and select “Report Properties.”
In the “Code” tab, enter the following VB.NET code:
vbnetCopy code
Public Function SplitValue(ByVal value As Decimal, ByVal isFirstHalf As Boolean) As Decimal
Dim splitValue As Decimal
If isFirstHalf Then
splitValue = Math.Truncate(value * 100) / 100
Else
splitValue = Math.Ceiling(value * 100) / 100
End If
Return splitValue
End Function
Calculated Fields:
In your dataset, create two calculated fields, one for the first half and another for the second half, using expressions like these:First half:
In your report, use these calculated fields for displaying the values.
This approach uses custom code to perform the splitting without rounding issues by explicitly truncating or ceiling the values as needed. It should ensure that you get the desired results even when the value doesn’t split evenly. Please ensure that you’ve correctly set the number formatting for these calculated fields to display two decimal places.
The error message “Local variable cannot have the same name as the function containing it” indicates that you have defined a local variable within your custom code with the same name as the custom function itself. This is a syntax error in VB.NET.
To fix this error, you should rename the local variable in your custom code so that it doesn’t have the same name as the function. Here’s the corrected code:
vbnetCopy code
Public Function SplitValue(ByVal value As Decimal, ByVal isFirstHalf As Boolean) As Decimal
Dim splitResult As Decimal
If isFirstHalf Then
splitResult = Math.Truncate(value * 100) / 100
Else
splitResult = Math.Ceiling(value * 100) / 100
End If
Return splitResult
End Function
In this corrected code, I’ve changed the local variable name from splitValue to splitResult to avoid the error. After making this change, the code should work without any issues.