converting string to decimal in c#
asp.net, c#, decimal, visual-studio-2010
Solution
I would use `Decimal.TryParse()`:
decimal parsedDecimal = 0;
string yourCurrency = "$779.99";
bool didParse = Decimal.TryParse(yourCurrency,
NumberStyles.Currency,
new CultureInfo("en-US"), out parsedDecimal);
if(didParse) {
// Parse succeeded
}
else {
// Parse failed
}
Problem
I am having some problems converting string to decimal values with decimal.parse. This is the line of code I have: ``` fixPrice = decimal.Parse(mItemParts.Groups["price"].Value.Replace("$", "").Replace(" ", "").Replace("usd", "")); ``` The value from which I am trying to convert is: '$779.99' Then once the parsing to decimal happens, I am getting this value: 77999. I would like to get 779.99 instead of 77999. Thanks in advance, Laziale Regex included: "@"\[^\""]+?)\""[^~]+?\]+?src=\""(?[^\""]+?)\""[^>]+?title=\""(?[^\""]+?)\""[^~]+?price\"">(?[^\<]+?)\<[^~]+?\(?[^\<]+?)\