3

I'm having an issue after submitting a form with the following error:

Input string was not in a correct format.

My code:

  Decimal intervalStart = Decimal.Parse(IntervalTime.Text);
  Decimal intervalTotal = intervalStart * 1000;
  string interval = intervalTotal.ToString();

I am trying to get a total in whole number rather than decimal, but the decimal is crucial in order to get that whole number (when multiplied by 1000). For example, my small application reads a video file and puts the total in seconds in the "IntervalTime.Text" box. This is then converted into milliseconds and becomes a whole number.

Video: 87.524 seconds. Multiply it by 1000, you get 87524. <- This is what I need but continue getting the above error.

enter image description here

enter image description here

ermalsh
  • 191
  • 1
  • 16

2 Answers2

2

The Decimal.Parse(String) parse the number which is in this format:

[ws][sign][digits,]digits[.fractional-digits][ws]

Be carefull that

  1. , is a culture-specific thousands separator symbol.
  2. . is a culture-specific decimal point symbol

This means that both 87,524 and 87.524 could be valid decimal number strings depending on the system culture.

Johnny
  • 8,939
  • 2
  • 28
  • 33
1

This doesn't work:

string b = "87.524";
Decimal intervalStart2 = Decimal.Parse(a);

But this does:

string a = "87,524";
Decimal intervalStart1 = Decimal.Parse(a);

Problem is in delimiter.

One of possible solutions could be:

string b = "87.524";
b = b.Replace(".", ",");
Decimal intervalStart = Decimal.Parse(b);

Also in this question it is shown how to define delimiter yourself:

decimal.Parse("87.524", new NumberFormatInfo() { NumberDecimalSeparator = "." })

Another way is to define specific CultureInfo:

decimal.Parse("87.524", new CultureInfo("en-US"));
Alex
  • 790
  • 7
  • 20
  • 1
    Thank you, the last two solutions you provided helped solve the issue. I will post the final code/answer in my original post above. Appreciate the help. – ermalsh Feb 12 '18 at 16:02