Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have created an SRSS report that shows height, width, and thickness which was in decimals and I have used custom VB code to convert those decimals to fractions. I am wondering if I can round those decimals in the Custom VB fraction code to get a smaller and appropriate fraction? Any suggestions?

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(Num)
        DecimalNumber = Num - Fix(Num)
        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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
208 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you mean you'd like to round the decimal received by the function before the value is converted, you could add the built in Round function where ever Num is referenced. The following can be used to transform the values -- the first parameter is the value you wish to round and the second specifies how many digits after the decimal point.

Decimal.Round(value,numbersRightOfDecimalPoint)

Since the values are Double datatypes, you will likely have to convert them to decimals with a ToDecimal function. The ToDecimal function takes the value to convert and a regional culture, which we can just set to Nothing so it'll just round as normal.

Convert.ToDecimal(value, culture)

From what I can see, you should only need to modify these two lines where Num is referenced to round the value throughout the function.

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

Which yields a result like this:

.501324532 >> 1/2

EDIT: On looking at your question again, it seems you MIGHT want to convert the decimals into fractions relating to measurements such as feet and inches. For that, refer to this link which has additional functions you can implement for that requirement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...