|
怎么感觉成了51testing老师和同学跟这示范教学了-。-!前两题貌似有人做了?那我来拜上第三题吧。。。版主老师也给指点指点呗~辛苦老师啦~
Option Explicit
'转换数据类型为用户自定义类型,如1234567890123456789,转换成1,234,567,890,123,456,789.00
'传入:待转换的数字
'传出:转换后的格式字符-。-!
Function FormatNum(varNum)
Dim varFormatNum, arrNum, varInt, varDecimal, varCount
On Error Resume Next
varFormatNum = varNum
'转换出错提示
If Err <> 0 Then
MsgBox "此字符串含有非数字的字符,请重新输入"
Err.Clear
End If
'将小数部分和整数部分分隔,分别处理
arrNum = Split(varFormatNum, ".")
'转换整数部分
varInt = arrNum(0)
varInt = ChangeFromLast(varInt)
'转换小数部分
varDecimal = arrNum(1)
varDecimal = ChangeFromFirst(varDecimal)
'合并处理结果
varFormatNum = varInt & "." & varDecimal
FormatNum = varFormatNum
End Function
'从最后一位开始,按三位逗号分割数字
Function ChangeFromLast(varNum)
Dim varInt, varResult
varInt = CStr(varNum)
If Len(varInt) < 3 Then
ChangeFromLast = varInt
Exit function
End If
Do
If varInt < 1000 Then
varResult = varInt & "," & varResult
Exit Do
ElseIf IsEmpty(varResult) Then
varResult = Right(varInt,3)
Else
varResult = Right(varInt,3) & "," & varResult
End If
varInt = Left(varInt, Len(varInt)-3)
Loop
ChangeFromLast = varResult
End Function
'从第一位开始,按三位逗号分隔数字
Function ChangeFromFirst(varNum)
Dim varInt, varResult, varCount
varInt = CStr(varNum)
If Len(varInt) <= 2 Then
For varCount = 0 To 2-Len(varInt)-1
varInt = varInt & "0"
Next
ChangeFromFirst = varInt
Exit function
End If
Do
If IsEmpty(varResult) Then
varResult = Left(varInt, 3)
ElseIf varInt < 1000 Then
varResult = varResult & "," & Left(varInt, 3)
Exit do
Else
varResult = varResult & "," & Left(varInt, 3)
End If
varInt = Mid(varInt, 4, Len(varInt))
Loop
ChangeFromFirst = varResult
End Function
MsgBox FormatNum("12345.1234")
[ 本帖最后由 dabeixiong 于 2009-8-19 22:50 编辑 ] |
|