Link to home
Start Free TrialLog in
Avatar of friskyweasel
friskyweasel

asked on

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" 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?


Avatar of aki4u
aki4u

int MyValue = Convert.ToDecimal(((Label) Page.FindControl("mylabel")).Text.Replace("$","").Replace("(","").Replace(")","").ToString().Trim());
try this one for...works 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
Avatar of friskyweasel

ASKER

hi aki4u - i hadn't thought about stringing the "Replace"s together like that...it'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"

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?

ASKER CERTIFIED SOLUTION
Avatar of aki4u
aki4u

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
i agree - well it's working just like i wanted it to now - thanks again for all your help
you're welcome