在自定义 VB 分数代码中舍入小数 [英] Rounding a decimal in Custom VB fraction code

查看:61
本文介绍了在自定义 VB 分数代码中舍入小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 SRSS 报告,以小数形式显示高度、宽度和厚度,并且我使用自定义 VB 代码将这些小数转换为分数.我想知道我是否可以在自定义 VB 分数代码中舍入这些小数以获得更小更合适的分数?有什么建议吗?

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

推荐答案

如果你的意思是你想在转换值之前对函数接收到的小数进行四舍五入,你可以添加内置的Round 函数,其中引用了 Num.以下可用于转换值——第一个参数是您要舍入的值,第二个参数指定小数点后的位数.

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)

由于值是 Double 数据类型,您可能必须使用 ToDecimal 函数将它们转换为小数.ToDecimal 函数接受要转换的值和区域文化,我们可以将其设置为 Nothing,这样它就会像往常一样四舍五入.

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)

据我所知,您只需要修改引用 Num 的这两行即可在整个函数中对值进行四舍五入.

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))

结果如下:

.501324532 >> 1/2

.501324532 >> 1/2

再次查看您的问题时,您似乎可能想要将小数转换为与英尺和英寸等测量值相关的分数.为此,请参阅 此链接 具有您可以为该要求实现的附加功能.

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.

这篇关于在自定义 VB 分数代码中舍入小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆