467,161 Members | 890 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

Beginner: Parse? Need to process currency input.

I am writing a program where the user inputs currency in US dollars. I want
the program to only accept valid currency input, converting the string into
the proper type of variable (double?), and to make sure there are no illegal
characters entered so that it doesn't make an error. I basically want it to
strip out any characters that aren't numbers, in case the user enters
something like "$34.23".

So far, in my hours of research today I have turned up some evidence that
this involves 'parse,' but I can't figure out the exact way to use it.

I am lost, but this is as far as I have made it so far. It doesn't work
("name 'numberstyles' not declared")

something like this?

Dim strUserInput As Double = Double.Parse(TextBox1.Text,
NumberStyles.Currency)

Nov 21 '05 #1
  • viewed: 4390
Share:
3 Replies
Josh,

Normally all should be done by the globalization in dotnet and especially by
the very strong conversion methods which are in VBNet as extra part to the
standard DotNet. This are full methods from the framework.

therefore

\\\
mydouble as double = cdbl(textbox1.text)
//
Should do the job for you.

When you set that in a try catch block as this

\\\
try
mydouble as double = cdbl(textbox1.text)
catch ex as message
messagebox.show("there should be enterend something strange " &
ex.tostring
end try
////

You will see that you catch almost every error.

(However I would take the decimal)

mydecimal as decimal = cdec(textbox1.text)

Here a link to those conversion functions.
http://msdn.microsoft.com/library/de...conversion.asp

I hope this helps?

Cor

"Josh" <jo*****@hotmail.com>
..
I am writing a program where the user inputs currency in US dollars. I
want
the program to only accept valid currency input, converting the string
into
the proper type of variable (double?), and to make sure there are no
illegal
characters entered so that it doesn't make an error. I basically want it
to
strip out any characters that aren't numbers, in case the user enters
something like "$34.23".

So far, in my hours of research today I have turned up some evidence that
this involves 'parse,' but I can't figure out the exact way to use it.

I am lost, but this is as far as I have made it so far. It doesn't work
("name 'numberstyles' not declared")

something like this?

Dim strUserInput As Double = Double.Parse(TextBox1.Text,
NumberStyles.Currency)

Nov 21 '05 #2
"Josh" <jo*****@hotmail.com> schrieb:
I am lost, but this is as far as I have made it so far. It doesn't work
("name 'numberstyles' not declared")

something like this?

Dim strUserInput As Double = Double.Parse(TextBox1.Text,
NumberStyles.Currency)


Your code is OK. Add a 'Imports System.Globalization' in the imports
section of your file...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #3
Josh,
If you are processing currency I would suggest using Decimal to avoid
precision loss and strange rounding problems. A sample code snipped would
be:

Decimal.Parse(TextBox1.Text, System.Globalization.NumberStyles.Currency)

Ron Allen
"Josh" <jo*****@hotmail.com> wrote in message
news:10*************@news20.forteinc.com...
I am writing a program where the user inputs currency in US dollars. I
want
the program to only accept valid currency input, converting the string
into
the proper type of variable (double?), and to make sure there are no
illegal
characters entered so that it doesn't make an error. I basically want it
to
strip out any characters that aren't numbers, in case the user enters
something like "$34.23".

So far, in my hours of research today I have turned up some evidence that
this involves 'parse,' but I can't figure out the exact way to use it.

I am lost, but this is as far as I have made it so far. It doesn't work
("name 'numberstyles' not declared")

something like this?

Dim strUserInput As Double = Double.Parse(TextBox1.Text,
NumberStyles.Currency)

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mark | last post: by
2 posts views Thread by probashi | last post: by
6 posts views Thread by jcnews | last post: by
5 posts views Thread by Navid Azimi | last post: by
29 posts views Thread by gs | last post: by
6 posts views Thread by =?Utf-8?B?RGF2aWRN?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.