Stupid newbie question--string formating/unformating


M

Mojomarc

I'm new still fairly new to programming, and working in C#. I've
written a small application as practice that takes in a string of
numbers, converts them to a decimal (subtotal), and based on another
text entry (txtCustomerType.Text) elsewhere on my form does some
calculations with that decimal (if the txtCustomerType.Text = "R", for
example, instead of getting no discount, the subtotal should reflect a
10% discount). The decimal is supposed to be formatted as currency
with respect to the form, so at the end of the whole calculation I put
this line in:


txtSubtotal.Text = subtotal.ToString("c");


So far, so good, and it builds and runs fine one time through. However
if I want to see how the same value would be calculated with a changed
customer type, I'm stuck with a currency-formated string for my
txtSubtotal.Text, and because all the calculations are based on
whatever value is in subtotal, it breaks because I no longer have a
decimal in there but a string.


I've tried noodling around with a bunch of different things including
trying to trim out the "$" character, but nothing seems to work to
convert it back to a plain string of numbers so I can run my
calculation over and over again. I'm sure this is a really basic
question, but I'm really stuck on this. Help!
 
Ad

Advertisements

M

Maqsood Ahmed

Hello,
Trim out '$' sign from the string and use decimal.Parse or
decimal.TryParse methods to get a decimal form string. see MSDN for
detailed documentation of these methods.

HTH. Cheers.
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
M

Mojomarc

Hello,
Trim out '$' sign from the string and use decimal.Parse or
decimal.TryParse methods to get a decimal form string. see MSDN for
detailed documentation of these methods.

HTH. Cheers.
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

I've never used the TryParse method. Right now, I have:

decimal subtotal = decimal.Parse(txtSubtotal.Text);

in there, but this is the statement that is breaking it when I try to
run through it again with the txtSubtotal.Text formated as a currency.

If TryParse would work, I'd love an example. The decimal.TryParse
(string, System.Globalization.NumberSytles,System.IFormatProvider,out
decimal) that VS is giving me is beyond what I know how to deal with at
this point.
 
M

Morten Wennevik

Hi Mojomarc

There is no Decimal.TryParse, only Double.TryParse.
You can however specify currency when parsing the text string

decimal d = Decimal.Parse(textBox1.Text, NumberStyles.Currency);

No need to trim out the $.



Hello,
Trim out '$' sign from the string and use decimal.Parse or
decimal.TryParse methods to get a decimal form string. see MSDN for
detailed documentation of these methods.

HTH. Cheers.
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

I've never used the TryParse method. Right now, I have:

decimal subtotal = decimal.Parse(txtSubtotal.Text);

in there, but this is the statement that is breaking it when I try to
run through it again with the txtSubtotal.Text formated as a currency.

If TryParse would work, I'd love an example. The decimal.TryParse
(string, System.Globalization.NumberSytles,System.IFormatProvider,out
decimal) that VS is giving me is beyond what I know how to deal with at
this point.
 
M

Mojomarc

Hi Mojomarc

There is no Decimal.TryParse, only Double.TryParse.
You can however specify currency when parsing the text string

decimal d = Decimal.Parse(textBox1.Text, NumberStyles.Currency);

No need to trim out the $.

Thanks, Morten--that was exactly what I was looking for and it works
perfectly.

Now on learning more about structured error handling....

Regards,
Marc
 
J

Jon Shemitz

Mojomarc said:
if I want to see how the same value would be calculated with a changed
customer type, I'm stuck with a currency-formated string for my
txtSubtotal.Text,

The simplest (and most efficient solution) is to store the subtotal as
a private member of the form class. Then you can just refer to it
directly, instead of having to parse a string.
 
Ad

Advertisements

M

Mojomarc

The simplest (and most efficient solution) is to store the subtotal as
a private member of the form class. Then you can just refer to it
directly, instead of having to parse a string.

Maybe I'm missing something, but the whole point of the txtSubtotal
textbox is to accept the user input of what the subtotal is. Can I
still do this and accept user input? I wouldn't think so, but then
again I'm a newbie so perhaps there would be a point in doing this.

Regards,
Marc
 
J

Jon Shemitz

Mojomarc said:
Maybe I'm missing something, but the whole point of the txtSubtotal
textbox is to accept the user input of what the subtotal is. Can I
still do this and accept user input? I wouldn't think so, but then
again I'm a newbie so perhaps there would be a point in doing this.

You're overwriting the user input with the calculated result? I
wouldn't have expected that, because it will tend to lead to nasty
recursion issues: calculation when the subtotal changes triggering
calculation when subtotal changes, &c.

I read quickly, and assumed you were generating the subtotal from a
series of user entered numbers.
 
Ad

Advertisements

M

Mojomarc

You're overwriting the user input with the calculated result? I
wouldn't have expected that, because it will tend to lead to nasty
recursion issues: calculation when the subtotal changes triggering
calculation when subtotal changes, &c.

I read quickly, and assumed you were generating the subtotal from a
series of user entered numbers.

Not this time--the subtotal is what the user enters. Basically, the
idea of this exercise is simply to calculate the final total based on
the subtotal and the discount that each subtotal range receives. Since
there is only really one step in this, I just wanted to change it so
that when a user entered a value "12345" it would change to
"$12,345.00" and apply the correct discount based on the customer type,
but then not break if, after the calculation was seen, the customer type
was changed and the value was recalculated.
 

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