Currency / Percentage to Decimal Conversion

hi all - quick question
I have a group of dynamically created label controls on my page, for which I need to 'assign to' and 'read from' currency and percentage values. A typical scenario would be that I write the value to the label via its Text property like so:
((Label) Page.FindControl("mylabel")).Text = MyCurrencyValue.ToString("c");

which would put something like this on the label:
$ xxxxx - if it's a positive value, or
($ xxxxx) - if it's a negative value

of course when i go to read these values back in, and need to convert them back to decimal form for computations...trying to do a Convert.ToDecimal would not work, because the "$" character caused the "input string in incorrect format" error....I was able to get around this by doing a replace on the "$" char like so...
int MyValue = Convert.ToDecimal(((Label) Page.FindControl("mylabel")).Text.Replace("$","").ToString().Trim());

but now that i'm running into some negative numbers, i'm finding that I will have to do the same thing with the "(" and ")" chars, plus add logic to convert the number to negative....this seems like taking the "long way" i'm wondering, how can i be able to take a value such as: ($ xxxx) on a label control, and have a quick 1 liner that reads that text value from the label control and converts it to decimal format without all the hassle of the string replacement?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

int MyValue = Convert.ToDecimal(((Label) Page.FindControl("mylabel")).Text.Replace("$","").Replace("(","").Replace(")","").ToString().Trim());
try this one for negative too

int MyValue = Convert.ToDecimal(((Label) Page.FindControl("mylabel")).Text.Replace("$","").Replace(" ","").Replace("(","-").Replace(")","").ToString().Trim());

also, you should use Convert.ToInt32 instead of Convert.ToDecimal
friskyweaselAuthor Commented:
hi aki4u - i hadn't thought about stringing the "Replace"s together like's a good suggestion, but what i'm really after is a way to natively handle the string that's on the label without having to use the replaces

in other words if i run some computations and come up with a decimal value for a variable called "MyMoneyVariable", say a negative 5 dollars and 25 cents, the decimal value is -5.25  - then when i get ready to display it on the page, i use something like MyMoneyVariable.ToString("c") - where the ("c") formats that -5.25 into ($5.25) when it's displayed on the page, what i'm really after is the reverse of .ToString("c") - something that takes a string value on a label and converts it back to a decimal number...and knows that if it's in the format ($5.25), that it should actually return -5.25 to the app

is there built in functionality in .NET to do that? seems like there should be but i haven't had any luck so far

oh btw - the "int" in all above posts should actually read "decimal"

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

friskyweaselAuthor Commented:
aki4u - that indeed works - sorry...simple mistake in my original post where i wrote "int MyValue" - it should have been "decimal MyValue" - everything i'm converting here is to and from decimal - "int" was just a mental typo

i'd like to leave the question open for a while longer, as what I'm really after is being able to completely get away from the whole replace string idea, but if there are no other suggestions made, the points will be yours since it works

what i'm really after is some built in functionality that performs the exact opposite of .ToString("c") when dealing with decimal values - just like i was saying above, if .ToString("c") will accept the decimal variable -5.25 and return the string ($5.25), then it seems logical that you should have a built in function that will take the string ($5.25) and return the decimal  -5.25

does that make sense? do you know if that functionality exists in .net?


try this one:

decimal MyValue = decimal.Parse(((Label) Page.FindControl("mylabel")).Text, System.Globalization.NumberStyles.Currency, System.Globalization.NumberFormatInfo.CurrentInfo);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
friskyweaselAuthor Commented:
perfect - points are yours and thanks a lot

any ideas on which would give better performance in a looping environment with quite a few label controls to loop through? the convert.todecimal method with string replaces versus the decimal.parse method?

For a few label controls performance will be same...I would use performance counters to spot the difference...decimal.Parse is much cleaner solution.
friskyweaselAuthor Commented:
i agree - well it's working just like i wanted it to now - thanks again for all your help
you're welcome
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.