四舍五入到 SSRS 中可变的小数位数

Rounding to a variable number of decimal places in SSRS

我正在尝试找到一种将 SSRS 中的字段四舍五入为动态小数位数的方法。我知道我可以动态地格式化它,它最终可能会变成那样,但是我的许多用户将把这个报告直接带到 Excel 并且想要有实际的数字字段。

我的 t-SQL 代码包括这些声明的变量:

      NumLong01                 DECIMAL(23,8)
    , NumLongDP01               INTEGER

此 table 中的第一组条目用于 headers 和舍入参数。所以我将这两个的值添加为:

 NULL
,4

然后我将实际的 table 值添加为:

 543210987654321.87654321
,NULL

这样我就可以将一整串数字放入 table 但它们的格式都必须相同。

运行 此查询产生:

当我转到 ReportBuilder 时,我的字段有这个表达式:

=Fields!NumLong01.Value

如果我想格式化一定数量的小数位,我可以这样做:

=Round(Fields!NumLong01.Value,2) 之类的。不过,我试图做的是让它变得动态:

=Round(Fields!NumLong01.Value,First(Fields!NumLongDP01.Value, "DataSet1"))

结果四舍五入到小数点后 0 位。随后我了解到——通过在我的领域中使用后半部分——这是一个 NULL 值。所以我尝试了 Sum 而不是 First——再次,只是在我的领域——并得到了我预期的 4。太好了,现在我有了我的号码,我只是把它作为我的四舍五入:

=Round(Fields!NumLong01.Value,Sum(Fields!NumLongDP01.Value, "DataSet1"))

唯一的问题是,这会产生错误。接下来我问自己是否出于某种原因它没有将其视为数字。所以我只是将它添加到我的领域。没问题。所以我真的不知道它在做什么。是不是觉得这个字段可能会变得很长,以至于四舍五入到非法的小数位数?

现在,我可以做到:

=IIf(Sum(Fields!NumLongDP01.Value, "DataSet1") = 8,Round(Fields!NumLong01.Value,8),IIf(Sum(Fields!NumLongDP01.Value, "DataSet1") = 7,Round(Fields!NumLong01.Value,7),IIf(Sum(Fields!NumLongDP01.Value, "DataSet1") = 6,Round(Fields!NumLong01.Value,6),IIf(Sum(Fields!NumLongDP01.Value, "DataSet1") = 5,Round(Fields!NumLong01.Value,5),IIf(Sum(Fields!NumLongDP01.Value, "DataSet1") = 4,Round(Fields!NumLong01.Value,4),IIf(Sum(Fields!NumLongDP01.Value, "DataSet1") = 3,Round(Fields!NumLong01.Value,3),IIf(Sum(Fields!NumLongDP01.Value, "DataSet1") = 2,Round(Fields!NumLong01.Value,2),IIf(Sum(Fields!NumLongDP01.Value, "DataSet1") = 1,Round(Fields!NumLong01.Value,1),Round(Fields!NumLong01.Value,0)))))))))

...这行得通。但这似乎是一种荒谬的方式。

我也很乐意table 只传递 t-SQL 中的四舍五入数字。但是后来我 运行 遇到了在报告中只显示一定数量的小数的问题,因为在数字格式中出于某种原因它不允许动态的小数位数。

如有任何想法,我们将不胜感激。

这并不是完成动态舍入或数字格式设置的详尽列表,因为您可以使用报告中的自定义代码或调整数据集的 SQL 查询来实现。

使用舍入:

The first set of entries in this table is for headers and rounding parameters. That way I can put a whole series of numbers into the table but they all have to be formatted the same way.

为避免在报表中构建需要 FirstSum 等聚合函数的表达式并生成随后必须删除的空白行,请考虑只输入每个小数点后的位数行而不是使用 header 行。即使看起来多余,成本(存储和表达式评估)也很低。

这意味着您可以使用 =Round(Fields!NumLong01.Value,Fields!NumLongDP01.Value) 作为 表达式 计算字段[,而不是使用: =Round(Fields!NumLong01.Value,First(Fields!NumLongDP01.Value, "DataSet1")) =56=] 在 DataSet1 或任何你的数据集被调用。

使用数字格式:

But then I run into the problem of showing only a certain number of decimals on the report, because in the number formatting it doesn't allow for a dynamic number of decimal places for some reason.

您可以为报告中的 NumLong01 字段定义自定义格式,并使用表达式使其动态化以构建您的自定义格式字符串。

  1. 打开 NumLong01 文本框或 tablix 字段的文本框属性
  2. 打开“数字”选项卡并从“类别”列表中select自定义
  3. 单击 fx 按钮并使用以下表达式 ="0." + StrDup(First(Fields!NumLongDP01.Value, "DataSet1"), "0")

使用您的示例数据,此表达式将生成自定义格式字符串 0.0000,它将 543210987654321.87654321 更改为 543210987654321.8765。供您参考,StrDup 将指定的字符串重复 X 次。

如果数字的小数部分小于所需的小数精度,此格式化字符串将用 0 填充它。如果不需要,请将要复制的字符串更改为 "#",如下所示:StrDup(First(Fields!NumLongDP01.Value, "DataSet1"), "#").

您也可以将此方法用作数据集中的计算字段,但如果您删除了 header 行,并且正在为每一行输入小数位,如前所述。这是因为你不能在计算字段表达式中使用聚合函数。

为此,请使用以下表达式将 计算字段 添加到您的数据集:=Format(Fields!NumLong01.Value, "0." + StrDup(Fields!NumLongDP01.Value, "0"))