3 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
string measurements = string.empty;measurements += string.Format(@" {{label: 'Measurement Name: {0}', children: [ {{label: 'Measured Value: {1}'}}, {{label: 'Min: {2}'}}, {{label: 'Max: {3}'}}, {{label: 'Measured String: {4}'}}, {{label: 'Expected String: {5}'}}, ] }},", drv["MeasurementName"] == null ? "NULL" : drv["MeasurementName"], drv["MeasuredValue"] == null ? "NULL" : drv["MeasuredValue"], drv["Min"] == null ? "NULL" : drv["Min"], drv["Max"] == null ? "NULL" : drv["Max"], drv["MeasuredString"] == null ? "NULL" : drv["MeasuredString"], drv["ExpectedString"] == null ? "NULL" : drv["ExpectedString"]);

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
問題
因?yàn)?/trans> textBox1.Text
只包含數(shù)字,但數(shù)字是 太大/太小 因?yàn)?/trans> textBox1.Text
包含: (A)非數(shù)字(除外) space
在開始/結(jié)束時(shí), -
(一開始)和/或 (B)您的代碼的應(yīng)用區(qū)域性中有1000個(gè)分隔符,而沒有指定 NumberStyles.AllowThousands
或者您指定 NumberStyles.AllowThousands
但錯(cuò)了 thousand separator
在文化和/或 (C)十進(jìn)制分隔符(不應(yīng)存在于 int
分析)
不確定的例子:
a = Int32.Parse("5000000000"); //5 billions, too largeb = Int32.Parse("-5000000000"); //-5 billions, too small//The limit for int (32-bit integer) is only from -2,147,483,648 to 2,147,483,647
a = Int32.Parse("a189"); //having a a = Int32.Parse("1-89"); //having - but not in the beginninga = Int32.Parse("18 9"); //having space, but not in the beginning or end
NumberStyles styles = NumberStyles.AllowThousands;a = Int32.Parse("1,189"); //not OK, no NumberStyles.AllowThousandsb = Int32.Parse("1,189", styles, new CultureInfo("fr-FR")); //not OK, having NumberStyles.AllowThousands but the culture specified use different thousand separator
NumberStyles styles = NumberStyles.AllowDecimalPoint;a = Int32.Parse("1.189", styles); //wrong, int parse cannot parse decimal point at all!
似乎不確定,但實(shí)際上是好的例子:
a = Int32.Parse("-189"); //having - but in the beginningb = Int32.Parse(" 189 "); //having space, but in the beginning or end
NumberStyles styles = NumberStyles.AllowThousands;a = Int32.Parse("1,189", styles); //ok, having NumberStyles.AllowThousands in the correct cultureb = Int32.Parse("1 189", styles, new CultureInfo("fr-FR")); //ok, having NumberStyles.AllowThousands and correct thousand separator is used for "fr-FR" culture
解
textBox1.Text
int
1234
使用 TryParse
而不是 Parse
若要確保非解析數(shù)字不會(huì)導(dǎo)致異常問題,請執(zhí)行以下操作。 檢查…的結(jié)果 TryParse
如果不處理的話 true
int val;bool result = int.TryParse(textbox1.Text, out val);if (!result) return; //something has gone wrong//OK, continue using val
- 3 回答
- 0 關(guān)注
- 1057 瀏覽
添加回答
舉報(bào)