为自定义VB代码插入If then语句

时间:2019-03-14 15:45:20

标签: vb.net reporting-services

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

分数:

FRaction

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

1 个答案:

答案 0 :(得分:0)

我猜你真的想要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))