SoFunction
Updated on 2025-03-07

C# implements a method of converting a thousandth string into a number

The examples in this article mainly implement the method of converting a thousandth of string into a number in C#, which has certain reference value for beginners in C#. The main contents are as follows:

The main function codes are as follows:

/// <summary>
/// Convert a thousandth string to a number/// Description: Convert a thousandth of such as "–111,222,333" to -111222333 numbers/// If the conversion fails, return -1/// </summary>
/// <param name="thousandthStr">Minitialites that need to be converted</param>/// <returns>Number</returns>public static int ParseThousandthString(this string thousandthStr)
{
  int _value = -1;
  if (!(thousandthStr))
  {
 try
 {
   _value = (thousandthStr,  |  | );
 }
 catch (Exception ex)
 {
   _value = -1;
   (("Add a thousandth string{0}Convert to numeric exception,reason:{0}", thousandthStr, ));
 }
  }
  return _value;
}

Unit tests are as follows:

[TestMethod()]
public void ParseThousandthStringTest()
{
  string _thousandthStr = "-111,222,333";
  int _expected1 = -111222333;
  int _actual1 = (_thousandthStr);
  (_expected1, _actual1);
}

Interested readers can test it yourself, hoping it will be helpful for everyone to learn C#!