Currency / Percentage to Decimal Conversion
Posted on 2006-04-08
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" around...so 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?