0

I'm having trouble with a very simple problem. Basically I'm taking a string, like "$30.00", and removing the '$' and then trying to convert it to a decimal. Although I keep getting an error stating that the string isn't in the correct format. Not sure what to do from here...

string freightstart = PostFregihtAmount.ToString();

freightstart = freightstart.TrimStart('$');

decimal freight = Decimal.Parse(freightstart);

I tried the following, per Alex Skiba's suggestion in the comments:

Decimal.Parse(freightstart, System.Globalization.CultureInfo.InvariantCulture);

This is the error I receive upon debugging:

enter image description here

This is a plugin for our onsite CRM 2011 system and some fields that I have to reference on the quote form are currency fields. When I query the data they come back as string formats, hence the example "$30.00". Long story short I need to convert it to decimal so that I may do my tax solution.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Anthony
  • 13
  • 4
  • 1
    What's an example of your input? – Dave Bish Jun 24 '14 at 16:56
  • What is the value of `freightstart`? – gunr2171 Jun 24 '14 at 16:56
  • Your code works for me with `"$30.00"` as the input... – Chris Mantle Jun 24 '14 at 16:57
  • No repro... Check the value of `freightstart` using a debugger. I suspect it is not what you think it is. – Robert Harvey Jun 24 '14 at 16:57
  • 2
    Probably you have localization issue here. Your regional settings are different from the input format. Try Decimal.Parse(freightstart, System.Globalization.CultureInfo.InvariantCulture); If it will help, read something about localization. – Alex Skiba Jun 24 '14 at 17:00
  • 1
    stupid to close the question as it's a real problem as @AlexSkiba stated – adjan Jun 24 '14 at 17:01
  • @addy2012, it might be a real problem, what what is the _question_? It boils down to "it doesn't work". We can't reproduce the issue with the input provided. I would have closed the question as "need more information", but Robert beat me to it. – gunr2171 Jun 24 '14 at 17:06
  • @gunr2171 You can't, because your system has not the same culture information. On every German system it would fail, too. – adjan Jun 24 '14 at 17:08
  • @addy2012, you have already provided more information than the OP has. The OP needs to tell us what the input and environment is. Yes, we can suspect that is the issue, but until the OP clarifies, this needs to stay closed. – gunr2171 Jun 24 '14 at 17:09
  • I've tried the way @AlexSkiba stated and this is what I see during debugging [link](http://s2.postimg.org/8xahmymdl/Convert_Problem.png) – Anthony Jun 24 '14 at 17:14
  • This is a simple, stupid problem but I can't seem to find the solution and @gunr2171 I am willing to provide anything that'll help me find the solution. – Anthony Jun 24 '14 at 17:16
  • This is a plugin for our onsite CRM 2011 system and some fields that I have to reference on the quote form are currency fields. When I query the data they come back as string formats, hence the example "$30.00". Long story short I need to convert it to decimal so that I may do my tax solution. – Anthony Jun 24 '14 at 17:19
  • @AkatsukiDiablo maybe you'll find the solution here http://stackoverflow.com/questions/1354924/how-do-i-parse-a-string-with-a-decimal-point-to-a-double – adjan Jun 24 '14 at 18:16
  • See the “Copy exception detail to the clipboard” in your screen shot? Click that and put the message in your post. – Dour High Arch Jun 24 '14 at 18:33

1 Answers1

1

Try explicitly allowing the decimal point when you parse the string, as well as the currency symbol (then you don't have to trim the $ before parsing):

var freight = Decimal.Parse("$30.00",
              NumberStyles.AllowCurrencySymbol | NumberStyles.AllowDecimalPoint);
Grant Winney
  • 65,241
  • 13
  • 115
  • 165