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.

14
  • 1
    What's an example of your input? Commented Jun 24, 2014 at 16:56
  • What is the value of freightstart? Commented Jun 24, 2014 at 16:56
  • Your code works for me with "$30.00" as the input... Commented Jun 24, 2014 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. Commented Jun 24, 2014 at 17:00
  • 1
    stupid to close the question as it's a real problem as @AlexSkiba stated Commented Jun 24, 2014 at 17:01

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.