|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Question about Strings to NumbersI have text fields which contain data that is formatted as
$10,240.99 $10.00 $0.25 25% And I need to obtain these Strings and convert them into numbers (so I can perform calculations). How can I do this? "Shayaan Siddiqui" <ssiddi***@ridgewood.k12.nj.us> wrote in message I always get a little bit (actually a lot!) confused by all these "locale news:C042F8A4.122D%ssiddiqui@ridgewood.k12.nj.us... >I have text fields which contain data that is formatted as > $10,240.99 > $10.00 > $0.25 > 25% > And I need to obtain these Strings and convert them into > numbers (so I can perform calculations). aware" things (such as different currency signs and thousands separators and decimal separators and the like) but if your string examples are representations of currency as it would be displayed in your own local, and if your code is going to be run on a machine set to the same local settings, then for the currency values you can simply use: Dim c1 As Currency c1 = CCur(Text1.Text) ' where Text1 contains a currency amount as showm .. . . and for the percentages (if you want to use Singles for them) you can use: Dim c2 As Single c2 = Val(Text1.Text) ' where Text2 contains a percentage as shown Other people here who know more about these "local aware" things will probably tell you better ways of doing it. Mike
Show quote
Hide quote
"Shayaan Siddiqui" <ssiddi***@ridgewood.k12.nj.us> wrote in message You'll need to test each string to see what characters it contains tonews:C042F8A4.122D%ssiddiqui@ridgewood.k12.nj.us... > I have text fields which contain data that is formatted as > > $10,240.99 > $10.00 > $0.25 > 25% > > > And I need to obtain these Strings and convert them into numbers (so I can > perform calculations). > > How can I do this? > determine which datatype to convert to. Look up the Instr() function and test for "$", ".", and "%". Then look up CCur(), CLng(), and CDbl() since those are the functions you'll use to do the casting. You'll also need to include error checking or data validation which can get a bit involved. Jim Edgar |
|||||||||||||||||||||||