By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Beginner: Parse? Need to process currency input.

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
"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

P: n/a
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.