比如港币兑换人民币的兑换率是1.06
但是兑换率是在数据库里设置的,却被设置反了。0.9434,这是人民币兑换港币的兑换率。
我用1除以0.9434=1.0599957600169599321602713589146,本来应该是用两位小数点的,四舍五入为1.06
请教VB中哪个函数可以实现呢?

解决方案 »

  1.   

    将x四舌5入精确到小数点后两位:
    int(x*100+0.5)/100int(1/0.9434*100+0.5)/100=1.06注:精确到小数点后3位小数
    int(x*1000+0.5)/1000
      

  2.   

    用int?是fix吧去掉小数是用fix吧
      

  3.   

    Function Round(Number, [NumDigitsAfterDecimal As Long])
        VBA.Math 的成员
        Round to a given number of decimal places
      

  4.   

    Int、Fix 函数
          返回参数的整数部分。语法Int(number)Fix(number)必要的 number 参数是 Double 或任何有效的数值表达式。如果 number 包含 Null,则返回 Null。说明Int 和 Fix 都会删除 number 的小数部份而返回剩下的整数。Int 和 Fix 的不同之处在于,如果 number 为负数,则 Int 返回小于或等于 number 的第一个负整数,而 Fix 则会返回大于或等于 number 的第一个负整数。例如,Int 将 -8.4 转换成 -9,而 Fix 将 -8.4 转换成 -8。Fix(number) μèóú£oSgn(number) * Int(Abs(number))
      

  5.   

    Round(1/0.9434,2)Csng(Format(1/0.9434,"0.00"))
      

  6.   

    round()四舍五入函数
    round(1/0.9434,2)
      

  7.   

    VB四舍五入函数修正版Private Function Round(ByVal Value As Double, ByVal ADigit As Byte) As Double
       Dim wValue As Double
       Dim tValue As Double
       Round = Value
       wValue = Value * 10# ^ ADigit + 0.5
       tValue = Int(wValue)
       If wValue - tValue > 0.99999999999999 Then tValue = tValue + 1
       Round = tValue / 10# ^ ADigit
    End Function
      

  8.   

    Private Function Round(ByVal Value As Double, ByVal ADigit As Byte) As Double
       Dim wValue As Double
       Dim tValue As Double
       Round = Value
       wValue = Value * 10# ^ ADigit + 0.5 * IIf(Value < 0, -1#, 1#)
       tValue = Int(wValue)
       If ((wValue - tValue) * IIf(Value < 0, -1#, 1#)) > 0.99999999999999 Then tValue = tValue + 1#
       Round = tValue / 10# ^ ADigit
    End Function
      

  9.   

    另外汇率应当是保留8位小数,而不是2位小数,否则误差性太大,最少也需要保留四位小数,即使是银行也是使用的100元为单位,同时保留2位小数,相当于4位小数,这仅仅只是相对于RMB,换而言之相对比日币等其它币种,那么这个精确度则太低了。特别是需要跟银行对帐时更是麻烦事,只有在最终的金额才保留2~4位小数。例如某天日币况英镑的汇率比值为1:0.0050382150323885252082119477670768,如果是保留两位小数,则成为了1:0.01,同1:0.0050或1:0.00503822,这个出入就太大了,若跟银行进行对帐,你的客户肯定会疯掉^_^
      

  10.   

    我的方案如下:
    1.用 format 函数
    2.用 formatcurrency 函数
    3.用 round 函数
    4.用 int(x*100+0.5)/100 可以精确到小数点后2位。
      

  11.   

    我的方案如下:
    1.用 format 函数
    2.用 formatcurrency 函数
    3.用 round 函数
    4.用 int(x*100+0.5)/100 可以精确到小数点后2位。
    ===================================================
    1.format返回的是string消耗过大
    2.同上
    3.VB当中的Round使用的是银行式四舍五入,例round(0.565,2)≈0.56
    4.对于部分数处理会由于浮点表示方式本来的不精确而使得结果不准确,就如我上面所给出的,需要做相应调整,下面给出一个相对简单点的:
    Public Function Round(Number As Currency, Optional Position As Integer = 4) As Currency
    '{CodeWizardStart}
        On Error GoTo CodeWizardErrorHandle
    '{CodeWizardEnd}
        'Round(expression [,numdecimalplaces])
            Round = IIf((Number * (10 ^ Position) - Int(Number * (10 ^ Position))) >= 0.5, Int(Number * (10 ^ Position)) + 1, Int(Number * (10 ^ Position))) / (10 ^ Position)
            
    '{CodeWizardStart}
    Exit Function
    CodeWizardErrorHandle:
        Err.Raise Err.Number,"Round",Err.Description
    '{CodeWizardEnd}
    End Function
    Private Function RoundDouble(Number As Double, Optional Position As Integer = 4) As Double'{CodeWizardStart}
        On Error GoTo CodeWizardErrorHandle
    '{CodeWizardEnd}
        'Round(expression [,numdecimalplaces])
        RoundDouble = IIf((Number * (10 ^ Position) - Int(Number * (10 ^ Position))) >= 0.5, Int(Number * (10 ^ Position)) + 1, Int(Number * (10 ^ Position))) / (10 ^ Position)'{CodeWizardStart}
    Exit Function
    CodeWizardErrorHandle:
        Err.Raise Err.Number,"RoundDouble",Err.Description 
    '{CodeWizardEnd}
    End Function