VB怎么去掉小数末尾的“0”
比如0.500 变成0.5 ,0.0300变成0.03
'自定义函数处理。思路:分别取小数点二边的数字串用整数来判断
Function MyFormat(sStr As String) As String
? ?Dim s1, s2 As String
? ?
? ?i = InStr(sStr, "。
")
? ?If i = 0 Then
? ? ? ?MyFormat = "" & Val(s1)
? ?Else
? ? ? ?s1 = Left(sStr, i - 1)
? ? ? ?s2 = Mid(sStr, i 1)
? ? ? ?If Val(s2) = 0 Then
? ? ? ? ? ?MyFormat = "" & Val(s1)
? ? ? ?Else
? ? ? ? ? ?MyFormat = "" & Val(s1) & Val("。
" & s2)
? ? ? ?End If
? ?End If
End Function
'例:
Private Sub Command1_Click()
? ?Text1。
Text = "00000。0000"
? ?Text2。Text = "00123。0000"
? ?Text3。Text = "00123。4560"
? ?
? ?Print MyFormat(Text1。
Text) ? ?'输出结果为 0
? ?Print MyFormat(Text2。Text) ? ?'输出结果为 123
? ?Print MyFormat(Text3。Text) ? ?'输出结果为 123。
456
End Sub。
Dim?a,?b,?c
Private?Sub?Command1_Click()
'c?=?Text1.Text?,这里C等于要转换的小数
If?Not?Left(c,?1)?=?0?Then
Print?Val(c)
Else
a?=?Val(c)
b?=?Str(a)
Print?Replace(Left(c,?1)? ?b,?"?",?"")
End?If
End?Sub
用Format函数
Print?Format(0.500,"0.0")'?结果为0.5
Print?Format(0.0300,"0.00")'?结果为0.03
VB只能出现,0.几几几的时候,没有最前面的0,怎样才能出现一个数字的小数末尾有零出现呢。我没碰上过,所以请你把你的代码写出来,我看看这个0是怎么出现的,我才能帮你去掉。
我就说不可能有这个的数字吗,是你把它变成字符串的,你在后面补了0,它当然要至少显示3位小数了,难道你没有好好看MSDN里关于Format函数的说明吗?!!
Format$(3.300000#,"0.###"),就可以保证最多显示3位小数,但不显示末尾的0,好好学习吧。
VB的print本身就自动会自动去掉尾部的0的
如
? a = "0.5000"
? Print Val(a)
结果为
.5
你是在什么情况下要去掉
答:应是不变的,比如:0。8和0。800大小相同, ①再如:0。4和0。40000000000000……后有无数的0,小数的大小相同。所以小数的末尾加零或减去无数的...详情>>
答:Rapid development in the Internet today, many people in the use of new network t...详情>>
答:If Len(Inet1.OpenURL(" ")) <> 0 Or_ Len(Inet1.OpenURL(" ")) <> 0 Then MsgBox "成功...详情>>