473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
3 4604
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Shawn | last post by:
Hello all, I apologize as I am sure this has probably been dealth with before... but I am doing an exercise from "Practical C Programming" and I have been unable to get it to work perfectly due to...
3
by: Mark | last post by:
How do you parse a currency string to a decimal? I'd like to avoid having to parse the number out, removing the $ manually. That sounds like a hack. There are times that this string will be...
2
by: probashi | last post by:
Hi, I am trying to the following: String s = "( 54.05)"; decimal d = decimal.Parse(s); when s = "( 54.05)" I what the value -54.05 and s = " 54.05" I what the value 54.05
3
by: Slonocode | last post by:
I have some textboxes bound to an access db. I wanted to format the textboxes that displayed currency and date info so I did the following: Dim WithEvents oBidAmt As Binding oBidAmt = New...
6
by: jcnews | last post by:
I am writing a 'wage calculator' program where the user inputs a dollar amount and then calculations are performed. I need to make sure that the input is only something like this: "$12.42", or...
5
by: Navid Azimi | last post by:
What's the best way to parse a currency string to a decimal given the possibility of multiple currencies? That is, my numeric string can be ($12.00) or -£12.00; in either case I want -12.00 to be...
10
by: Mike9900 | last post by:
Hello, I need a regular expression to match a currency with its symbol, for example Pound66.99 must return 66.99 or Pound(66.99) or Pound-66.99 or -66.99Pound return -66.99 or any other...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
6
by: =?Utf-8?B?RGF2aWRN?= | last post by:
Hello, I have an XML file generated from a third party application that I would like to parse. Ideally, I plan on having a windows service setup to scan various folders for XML files and parse the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.