Hi,
OK. This is a very easy problem - that I can't find an answer for. I'm sure I'll fell dumb when I (you) figure it out.
I'm converting a string to currency. Nothing happens. It returns the same string - but not in catch - it is NOT going into catch.. Here's my function:
samples:
input:16406, 136.54
output: 16406, 136.54
public string currencyConvert(string money)
{
string curr ="";
try
{
curr = string.Format("{0:C}", money);
}
catch
{
curr = money;
}
return curr;
}
Loading

Posted Jun 28, 2007, 7:24 AM EDT
AlanRank #669 · 3.6K reputationPosted Jun 28, 2007, 7:30 AM EDT
public string currencyConvert(string money)
{
decimal number;
decimal.TryParse(money, out number);
return string.Format("{0:C}", number);
}
Posted Jun 28, 2007, 7:40 AM EDT
Ingrid3 reputationPosted Jun 28, 2007, 8:28 AM EDT
AlanRank #669 · 3.6K reputationPosted Jun 28, 2007, 8:37 AM EDT
There's no need to be embarrassed, as this behaviour is not obvious :)
Incidentally, if you pass in a string which isn't numeric (let's say "123A.56"), then the TryParse()method will just make the 'number' variable zero and will give a return value of 'false'. I didn't try to catch this in my example by throwing an exception but you could, of course, do this if you wish.