3

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\"">(?[^\<]+?)\<[^~]+?\(?[^\<]+?)\

Laziale
  • 7,965
  • 46
  • 146
  • 262
  • It looks like you are using a Regular Expression to get the price, what is the expression that you are using? – CraigW Apr 13 '12 at 17:07
  • @CraigW Regex included. The final product I am getting from the regex is $779.99 – Laziale Apr 13 '12 at 17:08

4 Answers4

6

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
}
James Hill
  • 60,353
  • 20
  • 145
  • 161
  • 2
    `CurrentCulture` might not be the best choice. – Odys Apr 13 '12 at 17:13
  • 1
    You should check the bool return value of TryParse to see if the parsing succeeded. – Steve Wong Apr 13 '12 at 17:14
  • @Commenters, sometimes I think I can sneak a quick and dirty sample in as an answer, then I remember this is SO :). I've updated my answer to reflect your suggestions. – James Hill Apr 13 '12 at 17:18
  • 1
    But this doesn't help the OP, who (1) knows the input string is valid, and (2) has a successful parse that returns an incorrect value. – phoog Apr 13 '12 at 17:19
  • @JamesHill: No worries, you still got my vote :). If I can nitpick again, your comments are wrong :) – Steve Wong Apr 13 '12 at 17:19
  • @SteveWong, lol. They weren't before I edited my answer :). I need to stop multi-tasking. Apparently, I'm miserable at it! – James Hill Apr 13 '12 at 17:22
3

It appears that you are running this in a culture where '.' is the group separator, and ',' is the decimal separator. To get around that, use the Parse overload that takes a CultureInfo:

fixPrice = decimal.Parse(stringExpression, CultureInfo.InvariantCulture);

Also look into the NumberStyles enum so you don't have to worry about currency signs yourself:

fixPrice = decimal.Parse(stringExpression, NumberStyles.Currency, new CultureInfo("en-US"));
phoog
  • 42,068
  • 6
  • 79
  • 117
  • In this way it being made sure that the string format will always be correct. What about `TryParse` ? – Pankaj Apr 13 '12 at 17:13
  • @PankajGarg Of course, `TryParse` is preferable if the OP is not sure whether the input will be valid. – phoog Apr 13 '12 at 17:15
  • According to LINQPad, none of these samples work - `FormatException - Input string was not in a correct format`. – James Hill Apr 13 '12 at 17:25
  • @JamesHill the first sample is supposed to work with "779.99" (as in the OP's example), and for me, it does. I have corrected the second example (InvariantCulture doesn't use "$" as the currency symbol); thanks for pointing out the error. – phoog Apr 13 '12 at 17:32
2

Pass a CultureInfo instance of the culture you are parsing from.

CultureInfo inherits from IFormatProvider

edit:

Here is a sample for the conversion

Decimal.Parse(yourValue, NumberStyles.AllowCurrencySymbol |
                         NumberStyles.AllowDecimalPoint   |
                         NumberStyles.AllowThousands,
              CultureInfo.InvariantCulture);
Odys
  • 8,951
  • 10
  • 69
  • 111
1

This works for me:

string decStr = "$779.99";
CultureInfo ci = new CultureInfo("en-US");
decimal fixPrice = decimal.Parse(decStr, NumberStyles.Currency, ci);
Petr Abdulin
  • 33,883
  • 9
  • 62
  • 96