473,698 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Te xtBox1.Text,
NumberStyles.Cu rrency)

Nov 21 '05 #1
3 4628
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.t ext)
//
Should do the job for you.

When you set that in a try catch block as this

\\\
try
mydouble as double = cdbl(textbox1.t ext)
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.t ext)

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

I hope this helps?

Cor

"Josh" <jo*****@hotmai l.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(Te xtBox1.Text,
NumberStyles.Cu rrency)

Nov 21 '05 #2
"Josh" <jo*****@hotmai l.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(Te xtBox1.Text,
NumberStyles.Cu rrency)


Your code is OK. Add a 'Imports System.Globaliz ation' 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(T extBox1.Text, System.Globaliz ation.NumberSty les.Currency)

Ron Allen
"Josh" <jo*****@hotmai l.com> wrote in message
news:10******** *****@news20.fo rteinc.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(Te xtBox1.Text,
NumberStyles.Cu rrency)

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2264
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 problems with floating point arithmetic and I am looking for a way to solve it. See the code below... Given a certain amount of change (below $1.00) the program will tell you how many of each coin you will need to get that amount. The program...
3
24395
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 currency and others when it will be a text integer or decimal. //This bombs because of the string having an improper format. Decimal.Parse("$9,200.00") Thanks in advance! Mark
2
17661
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
3079
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 Binding("Text", Me.Ds1, "Items.BidAmt") txtBidAmt.DataBindings.Add(oBidAmt) Private Sub oBidAmt_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles oBidAmt.Format
6
3320
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 "$4", or "5.30", and have the output always be in this format: "$#.##". Currently, the output sometimes looks like this: "$54.321", or "$512.2". This is what I have come up with by researching in books and using the built-in help. It's actually...
5
3685
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 returned. I understand that this may be slightly difficult given non-symbol currency strings (F or Kr) but I figured that the CultureInfo should be able to take care of it somehow. The closest solution I came up with, short of iterating through...
10
10192
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 combination return the decimal number. I have created a regular expression, but it seems that it does not work if the number is Pound66.99 but it works if the sign is after the number: public static Decimal ConvertToDecimal(String str)
29
2889
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
2971
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 file, then spit out totals. Since I haven't worked with XML too much in C#, I'm trying to develop a structured and easy-to-read way to parse the file. Essentially, I would like to read the file and add the "BatchTktAmountfor any...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.