關(guān)鍵代碼:
/// <summary> /// 將千分位字符串轉(zhuǎn)換成數(shù)字 /// 說(shuō)明:將諸如"–111,222,333的千分位"轉(zhuǎn)換成-111222333數(shù)字 /// 若轉(zhuǎn)換失敗則返回-1 /// </summary> /// <param name="thousandthStr">需要轉(zhuǎn)換的千分位</param> /// <returns>數(shù)字</returns> public static int ParseThousandthString(this string thousandthStr) { int _value = -1; if (!string.IsNullOrEmpty(thousandthStr)) { try { _value = int.Parse(thousandthStr, NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign); } catch (Exception ex) { _value = -1; Debug.WriteLine(string.Format("將千分位字符串{0}轉(zhuǎn)換成數(shù)字異常,原因:{0}", thousandthStr, ex.Message)); } } return _value; }
單元測(cè)試:
[TestMethod()] public void ParseThousandthStringTest() { string _thousandthStr = "-111,222,333"; int _expected1 = -111222333; int _actual1 = StringToolV2.ParseThousandthString(_thousandthStr); Assert.AreEqual(_expected1, _actual1); }
希望有所幫助!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注