【问题标题】:Inserting a If then statement for custom VB code为自定义 VB 代码插入 If then 语句
【发布时间】:2019-03-14 15:45:20
【问题描述】:

我查询了一份报告,该报告显示了十进制格式的窗口的宽度、高度和厚度,直到我在 SSRS 中插入自定义 VB 代码以将这些小数更改为分数,并且一个特定的分数假设为“ 5/23”显示为“39/250”,我想知道我可以输入一个 IF 语句来让 39/250 表示 5/23。

分数:

Function GetFraction(ByVal Num As Double) As String
    If Num = 0# Then
        GetFraction = "None"
    Else
        Dim WholeNumber As Integer
        Dim DecimalNumber As Double
        Dim Numerator As Double
        Dim Denomenator As Double
        Dim a, b, t As Double

        WholeNumber = Fix(Decimal.Round(Convert.ToDecimal(Num,Nothing), 3))
        DecimalNumber = Decimal.Round(Convert.ToDecimal(Num,Nothing),3) - Fix(Decimal.Round(Convert.ToDecimal(Num,Nothing),3))
        Numerator = DecimalNumber *10 ^ (Len(CStr(DecimalNumber)) - 2)
        Denomenator = 10 ^ (Len(CStr(DecimalNumber)) - 2)
        If Numerator = 0 Then
            GetFraction = WholeNumber
        Else
            a = Numerator
            b = Denomenator
            t = 0

            While b <> 0
                t = b
                b = a Mod b
                a = t
            End While
            If WholeNumber = 0 Then
                GetFraction = CStr(Numerator / a) & "/" & CStr(Denomenator / a)
            Else
                GetFraction = CStr(WholeNumber) & " " & CStr(Numerator / a) & "/" & CStr(Denomenator / a)
            End If
        End If
    End If
End Function

【问题讨论】:

  • 似乎是一个精度问题,您应该将所有变量设置为 Decimal 而不是 Double。
  • 我已经这样做了,但我仍然有同样的问题
  • 由于您已将我的answer 应用于您之前的问题,但没有将mark it as an answer 应用于您之前的问题,我不确定我是否愿意提供帮助。如果您按预期使用 SO 那就太好了,也许您会有更多人愿意提供帮助?我想说你可能想使用类似IF GetFraction = "39/250" 的东西,但这些分数实际上是完全不同的十进制值。该分数是多少十进制值?
  • 1563/10000的十进制值为0.1563,但假设为5/23。 @史蒂夫-o169

标签: vb.net reporting-services


【解决方案1】:

我猜你真的想要 5/32,而不是 5/23,因为 5/23 = 0.2173913043478261 而 5/32 = 0.15625 这似乎是你想要的数字。您需要做的是将四舍五入调整到小数点后五位。我相信这两条线只需要3s切换到5s。在这种情况下,您可能需要更像 6,但由于 5/32 的小数点右边有 5 位,我想这会解决您的问题。

WholeNumber = Fix(Decimal.Round(Convert.ToDecimal(Num,Nothing), 5))
DecimalNumber = Decimal.Round(Convert.ToDecimal(Num,Nothing),5) - Fix(Decimal.Round(Convert.ToDecimal(Num,Nothing),5))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多