大家好,问大家一个问题~~~~~~~~~~~~~~~~~~~~~~~~~
我的量具的数据默认是MM(两位小数)
我想把数据转换成英制的(三位小数)
但是数据出来还是两位,怎么该阿??
        data = StrConv(inputbuffer, vbUnicode)
        Textcom1.Text = Left$(data, 1) & CStr(Val(Mid$(data, 3, 6)) / 100)
        dblValue = Val(Left$(data, 1) & "1") * Val(Mid$(data, 3, 6)) / 100
我是这样改的...............................
        data = StrConv(inputbuffer, vbUnicode)
        Textcom1.Text = Left$(data, 1) & CStr(Val(Mid$(data, 3, 6)) / 1000)
        dblValue = Val(Left$(data, 1) & "1") * Val(Mid$(data, 3, 6)) / 1000/25.4
结果只是显示两位小数,其实我想要的最好是四位小数
但是我改了Textcom1.Text = Left$(data, 1) & CStr(Val(Mid$(data, 3, 6)) / 10000)之后就只是显示全0了

解决方案 »

  1.   

    data是不是默认mm的数据?如果是就可以使用如下代码
    textcom1.text=format(val(data)*25.4,"0.000")
    需要保留多少位小数直接修改上面代码中的小数点后的0即可。
      

  2.   

    data是默认mm的数据.
    但是代码不行~~~~~我是把这句放在
    Textcom1.Text = Left$(data, 1) & CStr(Val(Mid$(data, 3, 6)) / 1000) 后面
    textcom1.text=format(val(data)*25.4,"0.000")
      

  3.   

    Textcom1.Text = Left$(data, 1) & CStr(Val(Mid$(data, 3, 6)) / 1000)
    '需要继续计算的话就放在这儿
    textcom1.text=format(textcom1.text,"0.000")
      

  4.   

    data为String,我的意思是这样的当我点击单位选择的时候,TEXTCOM1显示的是英制分数了,我的数据是通讯状态
      

  5.   

    private m_bSwitchUnit as boolean '是否变更单位'private sub cmd单位选择_click()
        m_bSwitchUnit = not m_bSwitchUnit
        txtUnit = iif(m_bSwitchUnit, "IN", "MM")
        '假定你的读数频率很高,当前的数值就不换算了'
    end subPrivate Sub MSComm1_OnComm()
        ...
        data = StrConv(inputbuffer, vbUnicode) 
        dblValue = Val(Left$(data, 1) & "1") * Val(Mid$(data, 3, 6)) / 100 
        if m_bSwitchUnit then
            Textcom1.Text = Format$(dblValue / 25.4, "0.000")
        else
            Textcom1.Text = Format$(dblValue, "0.00")
        end if 
        ...
    End Sub
      

  6.   

    ~~呃,,,,单击单位选择按钮没有反应~~~另外.m_bSwitchUnit是什么意思阿??
      

  7.   

    private sub cmd单位选择_click()
        dim dblValue as double
        m_bSwitchUnit = not m_bSwitchUnit
        txtUnit = iif(m_bSwitchUnit, "IN", "MM")
        dblValue = Val(Textcom1)
        if m_bSwitchUnit then
            Textcom1.Text = Format$(dblValue / 25.4, "0.000")
        else
            Textcom1.Text = Format$(dblValue * 25.4, "0.00")
        end if 
    end sub
      

  8.   

    还是不行,一点就变回来了~~~~~~~这是代码
    Private Sub cmd单位选择_click()
        Dim dblValue As Double
        m_bSwitchUnit = Not m_bSwitchUnit
        txtUnit = IIf(m_bSwitchUnit, "IN", "MM")
        dblValue = Val(Textcom1)
        If m_bSwitchUnit Then
            Textcom1.Text = Format$(dblValue / 25.4, "0.000")
        Else
            Textcom1.Text = Format$(dblValue * 25.4, "0.00")
        End If
    End SubPrivate Sub MSComm1_OnComm()
       Dim inputbuffer As Variant
       Dim data As String
       Dim dblValue As Double
       Select Case MSComm1.CommEvent
          Case comEvReceive
               inputbuffer = MSComm1.Input()
               data = StrConv(inputbuffer, vbUnicode)
               dblValue = Val(Left$(data, 1) & "1") * Val(Mid$(data, 3, 8)) / 100
               Textcom1.Text = FormatNumber(dblValue, , vbTrue)
        End Select
    End Sub
    Private Sub Form_Load()
        MSComm1.CommPort = 1
        MSComm1.Settings = "9600, n, 8, 1"
        MSComm1.RThreshold = 10
        MSComm1.InputMode = comInputModeBinary
        MSComm1.PortOpen = True
        Text2.Text = "MM"
    End Sub
    另外,它通讯不稳定,又时候要点其他的按键或者我把窗口最大.小化后数据才出来,你帮看看,哪里的问题??
      

  9.   

    我7楼给的 MSComm1_OnComm() 部分没合并进去!?
      

  10.   

    合了还是一样,一松开鼠标只能看到一下
    Private m_bSwitchUnit As Boolean '是否变更单位'
    Private Sub command8_click()
    'Private Sub cmd单位选择_click()
        'Dim dblValue As Double
        m_bSwitchUnit = Not m_bSwitchUnit
        txtUnit = IIf(m_bSwitchUnit, "IN", "MM")
        dblValue = Val(Textcom1)
        If m_bSwitchUnit Then
           Textcom1.Text = Format$(dblValue / 25.4, "0.000")
        Else
           Textcom1.Text = Format$(dblValue * 25.4, "0.00")
       End If
    End Sub
    Private Sub Form_Load()
        MSComm1.CommPort = 1
        MSComm1.Settings = "9600, n, 8, 1"
        MSComm1.RThreshold = 10
        MSComm1.InBufferCount = 0
        MSComm1.InputMode = comInputModeBinary
        MSComm1.PortOpen = True
        Text2.Text = "MM"
    End SubPrivate Sub MSComm1_OnComm()
       Dim inputbuffer As Variant
       Dim data As String
       Dim dblValue As Double
       Select Case MSComm1.CommEvent
           Case comEvReceive
               inputbuffer = MSComm1.Input
               data = StrConv(inputbuffer, vbUnicode)
               Textcom1.Text = Left$(data, 1) & CStr(Val(Mid$(data, 3, 6)) / 100)
               dblValue = Val(Left$(data, 1) & "1") * Val(Mid$(data, 3, 6)) / 100
               'Textcom1.Text = FormatNumber(dblValue, , vbTrue)
               If m_bSwitchUnit Then
            Textcom1.Text = Format$(dblValue / 25.4, "0.000")
        Else
            Textcom1.Text = Format$(dblValue, "0.00")
        End If