Word Document Properties


Listing 4-9 Converting between Numbers and Strings



Download 8,82 Mb.
Pdf ko'rish
bet92/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   88   89   90   91   92   93   94   95   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Listing 4-9
Converting between Numbers and Strings
Public Sub NumberConvert()
‘ Create some variables for use in conversion.
Dim MyInt As Integer
Dim MySgl As Single
Dim MyStr As String
‘ Conversion between Integer and Single is direct
‘ with no data loss.
MyInt = 30
MySgl = MyInt
MsgBox “MyInt = “ + CStr(MyInt) + _
vbCrLf + “MySgl = “ + CStr(MySgl), _
vbOKOnly, _
“Current Data Values”
‘ Conversion between Single and Integer is also direct
‘ but incurs data loss.
MySgl = 35.01
MyInt = MySgl
MsgBox “MyInt = “ + CStr(MyInt) + _
vbCrLf + “MySgl = “ + CStr(MySgl), _
vbOKOnly, _
“Current Data Values”
‘ Conversion between a String and a Single or an
‘ Integer can rely on use of a special function. The
‘ conversion can also incur data loss.
MyStr = “40.05”
96
Part II: Learning the Ropes 
09_046500 ch04.qxp 12/5/06 5:54 PM Page 96


MyInt = CInt(MyStr)
MySgl = CSng(MyStr)
MsgBox “MyInt = “ + CStr(MyInt) + _
vbCrLf + “MySgl = “ + CStr(MySgl), _
vbOKOnly, _
“Current Data Values”
‘ Conversion between a Single or Integer and a String
‘ can rely on use of a special function when making a
‘ direct conversion. The conversion doesn’t incur any
‘ data loss.
MyInt = 45
MySgl = 45.05
MyStr = MyInt
MsgBox MyStr, _
vbOKOnly, _
“Current Data Values”
‘ You must use a special function in mixed data
‘ situations.
MyStr = “MyInt = “ + CStr(MyInt) + _
vbCrLf + “MySgl = “ + CStr(MySgl)
MsgBox MyStr, _
vbOKOnly, _
“Current Data Values”
End Sub
The code begins by declaring an 
Integer
, a 
Single
(a real number), and a
String
. Although the code relies on these three data types, the principles
shown apply to any of the data types. Notice that you can perform direct 
conversion between numeric types without relying on a function. An integer
value can always convert to a real number without data loss. Be careful about
going the other way, though, because you can run into problems with data
loss. The conversion process drops the decimal value but uses proper round-
ing, as do the 
CInt
and 
CLng
functions.
Conversion from a string to a numeric value might not require the special
functions shown in the code. Use the conversion functions, as shown (
CInt
for 
Integer
conversion and 
CSng
for 
Single
conversion), to ensure that
VBA converts the data correctly. Try changing the source code so that it
reads 
MyInt = MyStr
— it works as normal in this case, but this behavior
isn’t guaranteed.
The code also shows that you can assign a numeric value directly to a string
as long as that’s the only assignment that you make. Always use the correct
conversion function when you work with mixed data types. Read the Type
Conversion Functions help topic to see other data conversion functions.
97

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   88   89   90   91   92   93   94   95   ...   207




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish