Beginner: Parse? Need to process currency input.

J

Josh

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)
 
C

Cor Ligthert

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/default.asp?url=/library/en-us/vblr7/html/vagrptypeconversion.asp

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

Josh said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top