问题描述
我有一个值为十进制或字符串的值.样品
I have a value that will either be a decimal or string. Sample
0.41
0.91
"0 / 2"
0.75
我现在的表情是=IIF(IsNumeric(Fields!currentRate.Value), Format(CDBL(Fields!currentRate.Value), "P2"), Fields!currentRate.Value)
My current expression is=IIF(IsNumeric(Fields!currentRate.Value), Format(CDBL(Fields!currentRate.Value), "P2"), Fields!currentRate.Value)
这正确地返回格式为百分比的小数,但是字符串仅显示#Error.我尝试在 IIF 语句中处理各种逻辑并使用 Switch 代替.然而,小数总是正确显示为百分比,而字符串仅显示 #Error.
This properly returns the decimals formatted as a percentage, however the strings are only showing #Error. I've tried messing with various logic in the IIF statement and using a Switch instead. However the decimals always properly show as a percent, while the string only shows #Error.
是否可以在保持数值格式的同时在同一列中同时显示数值和字符串值?
Is it possible to display both numeric and string values in the same column while maintaining formatting on the numeric value?
推荐答案
该错误与 CDbl
函数在尝试将字符串列转换为数字时抛出异常有关.是的,我知道您首先检查它是否是数字,但 IIF
不是语言结构,它是一个 函数 并且作为一个函数,它在传递之前评估其所有参数他们的功能.这意味着 True 和 False 参数都会被计算,即使其中一个会被丢弃,并且当它在字符串上计算 CDbl
时会抛出错误.
The error relates to the CDbl
function throwing an exception when trying to convert columns that are strings to a number. Yes, I know you're checking if it is numeric first but IIF
is not a language construct, it is a function and as a function it evaluates all its parameters before passing them to the function. This means that both the True and False parameters get calculated even though one will be discarded and when it calculates CDbl
on a string it throws an error.
试试Val
函数.它的好处是在传递非数字数据时不会抛出错误 - 它只是尽其所能将其转换为数字.
Try the Val
function. It has the benefit of not throwing errors when it gets passed non-numeric data - it just does the best it can to convert it to a number.
=IIF(IsNumeric(Fields!currentRate.Value), Format(Val(Fields!currentRate.Value), "P2"), Fields!currentRate.Value)
这篇关于当值为非数字时,SSRS IIF 语句显示 #Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!