Home All Groups Group Topic Archive Search About

Question about Strings to Numbers

Author
19 Mar 2006 5:01 PM
Shayaan Siddiqui
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?

Author
19 Mar 2006 5:52 PM
Mike Williams
"Shayaan Siddiqui" <ssiddi***@ridgewood.k12.nj.us> wrote in message
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).

I always get a little bit (actually a lot!) confused by all these "locale
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
Author
19 Mar 2006 6:05 PM
Jim Edgar
Show quote Hide quote
"Shayaan Siddiqui" <ssiddi***@ridgewood.k12.nj.us> wrote in message
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).
>
> How can I do this?
>

You'll need to test each string to see what characters it contains to
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