Rounding to nearest 0.5 in SSRS report

SQLNew 2021 96 Reputation points
2021-06-21T18:57:57.76+00:00

Hi

I am trying to round to the nearest 0.5 half. HELP.....I am going round in circles!!

I am adding 4 numbers together and dividing the total to give me the nearest 0.5;

Example:

(5+4+4+4) = 17 / 4 result is 4.25. I want this to show as 4.5 but this shows as 4.

Another example: (5.5+5+5+5.5) = 21 / result is 5.25 and I would like to round this to 5.5 but this rounds down to 5.

Also if the resulting value is for example 4.24 then this should be rounded to 4.

Here is the expression in the report:

=ROUND(
IIF(CDec(IIF((Max(IIF(Fields!intReportCycleID.Value = 50 AND Fields!txtResultName.Value = "sP", Fields!txtResult.Value, Nothing))) <> "",
Max(IIF(Fields!intReportCycleID.Value = 50 AND Fields!txtResultName.Value = "sP", Fields!txtResult.Value, Nothing)), 0)) +
CDec(IIF((Max(IIF(Fields!intReportCycleID.Value = 50 AND Fields!txtResultName.Value = "mP", Fields!txtResult.Value, Nothing))) <> "",
Max(IIF(Fields!intReportCycleID.Value = 50 AND Fields!txtResultName.Value = "mP", Fields!txtResult.Value, Nothing)), 0))

0,

(CDEC(IIF((Max(IIF(Fields!intReportCycleID.Value = 41 And Fields!intReportCycleTerm.Value = 1, Fields!txtResult.Value, Nothing))) <>"",
Max(IIF(Fields!intReportCycleID.Value = 41 AND Fields!intReportCycleTerm.Value = 1, Fields!txtResult.Value, Nothing)),0)) * 0.25)
+
(CDEC(IIF((Max(IIF(Fields!intReportCycleID.Value = 47 And Fields!intReportCycleTerm.Value = 2, Fields!txtResult.Value, Nothing))) <>"",
Max(IIF(Fields!intReportCycleID.Value = 47 AND Fields!intReportCycleTerm.Value = 2, Fields!txtResult.Value, Nothing)),0)) * 0.25)
+
((CDec(IIF((Max(IIF(Fields!intReportCycleID.Value = 50 AND Fields!txtResultName.Value = "sP", Fields!txtResult.Value, Nothing))) <> "",
Max(IIF(Fields!intReportCycleID.Value = 50 AND Fields!txtResultName.Value = "sP", Fields!txtResult.Value, Nothing)), 0)) * 0.25)
+
CDec(IIF((Max(IIF(Fields!intReportCycleID.Value = 50 AND Fields!txtResultName.Value = "mP", Fields!txtResult.Value, Nothing))) <> "",
Max(IIF(Fields!intReportCycleID.Value = 50 AND Fields!txtResultName.Value = "mP", Fields!txtResult.Value, Nothing)), 0)) * 0.25)
,
(CDEC(IIF((Max(IIF(Fields!intReportCycleID.Value = 41 And Fields!intReportCycleTerm.Value = 1, Fields!txtResult.Value, Nothing))) <>"",
Max(IIF(Fields!intReportCycleID.Value = 41 And Fields!intReportCycleTerm.Value = 1, Fields!txtResult.Value, Nothing)),0)) * 0.50)
+
(CDEC(IIF((Max(IIF(Fields!intReportCycleID.Value = 47 And Fields!intReportCycleTerm.Value = 2, Fields!txtResult.Value, Nothing))) <>"",
Max(IIF(Fields!intReportCycleID.Value = 47 And Fields!intReportCycleTerm.Value = 2, Fields!txtResult.Value, Nothing)),0)) * 0.50)
)
* 2)/2

Thank you in advance and hope someone can help.

SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,064 questions
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. SQLNew 2021 96 Reputation points
    2021-06-22T06:00:02.803+00:00

    I ended up updating my expression box with '* 2, 0, MidpointRounding.AwayFromZero)/2' and it solved the poblem.


1 additional answer

Sort by: Most helpful
  1. Olaf Helper 47,441 Reputation points
    2021-06-22T05:48:51.433+00:00

    (5+4+4+4) = 17 / 4 result is 4.25. I want this to show as 4.5 but this shows as 4.

    It's because you calculate with integer values and so you get an integer as result = 4. You have to use decimal values instead.
    To get the next "half" value, you multiply with 2.0 (decimal!), round it and devide it by 2.0 (decimal!), like

    ;with demo as
        (select 1.1 as val union all
         select 4.25 union all
         select 5.55 union all
         select 7.88)
    select val, round(val * 2.0, 0) / 2.0 as NextHalf
    from demo
    
    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.