如何用代码实现以下几个字符串相加

linzhongde 2004-05-07 12:27:40
有几个字符串,如下:
8/12.32% , 12/23.32% , /0% ,34/43.32%

现要实现符号"/"之前的数相加,字符“/”后的百分比相加,如何实现?
即结果为 8+12+0+34 和 12.32%+23.32%+0%+43.32%
...全文
58 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
broown 2004-05-07
  • 打赏
  • 举报
回复
楼上的板扎!
northwolves 2004-05-07
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
MsgBox SUM(" 8/12.32% , 12/23.32% , /0% ,34/43.32%")
End Sub
Function SUM(ByVal X As String) As String
Dim TEMP, TEMP1 As Double, TEMP2 As String, TEMP3 As Double, I As Long
TEMP = Split(X, ",")
For I = 0 To UBound(TEMP)
TEMP1 = TEMP1 + Val(Trim(Split(TEMP(I), "/")(0)))
TEMP2 = Trim(Split(TEMP(I), "/")(1))
TEMP2 = Left(TEMP2, Len(TEMP2) - 1)
TEMP3 = TEMP3 + TEMP2
Next
SUM = TEMP1 & "/" & TEMP3 & "%"
Set TEMP = Nothing
End Function
roger_xiong 2004-05-07
  • 打赏
  • 举报
回复
哦,有人说过了split啊?晕哦。呵呵,慢了。
roger_xiong 2004-05-07
  • 打赏
  • 举报
回复
还可以用split分,这样比较快一些。
Dim strTemp(3) As String
Dim intResult As Double
Dim i As Integer
Dim temp() As String

strTemp(0) = "8/12.32%"
strTemp(1) = "12/23.32%"
strTemp(2) = "/0%"
strTemp(3) = "34/43.32%"

For i = 0 To 3
temp = Split(strTemp(i), "/")
intResult = intResult + Val(temp(0)) + Val(Left(temp(1), Len(temp(1)) - 1)) / 100
Next
MsgBox intResult
roger_xiong 2004-05-07
  • 打赏
  • 举报
回复
Dim strTemp(3) As String
Dim intResult As Double
Dim i As Integer
Dim temp As String

strTemp(0) = "8/12.32%"
strTemp(1) = "12/23.32%"
strTemp(2) = "/0%"
strTemp(3) = "34/43.32%"

For i = 0 To 3
'取前面的数字
intResult = intResult + Val(Left(strTemp(i), InStr(1, strTemp(i), "/") - 1))
'取后面的百分数
temp = Mid(strTemp(i), InStr(1, strTemp(i), "/") + 1)
temp = Left(temp, Len(temp) - 1)
intResult = intResult + Val(temp) / 100
Next
MsgBox intResult
danielinbiti 2004-05-07
  • 打赏
  • 举报
回复
用split分割字符实现

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧