tryparse

Best (safest) way to convert from double to int

谁说胖子不能爱 提交于 2020-07-03 01:51:14
问题 I'm curious as to the best way to convert a double to an int. Runtime safety is my primary concern here (it doesn't necessarily have to be the fastest method, but that would be my secondary concern). I've left a few options I can come up with below. Can anyone weigh in on which is best practice? Any better ways to accomplish this that I haven't listed? double foo = 1; int bar; // Option 1 bool parsed = Int32.TryParse(foo.ToString(), out bar); if (parsed) { //... } // Option 2 bar = Convert

Why does double.TryParse(“6E02”, out tempDouble) return true?

依然范特西╮ 提交于 2020-01-17 18:09:34
问题 It took me a day to figure out the problem that one of the if statement returns true for a string value. We are parsing to check whether the value is a number or a string. I found out that this statement used and when the string value comes in as 6E02 the statement return true that this is a double value. var double temp; var val ="6E02" result = double.TryParse(val, out temp) How can I fix this issue to return the result false for strings like (Number)E0(Number) Easy way I believe to check

pros and cons of TryCatch versus TryParse

此生再无相见时 提交于 2020-01-13 08:08:56
问题 What are the pros and cons of using either of the following approaches to pulling out a double from an object? Beyond just personal preferences, issues I'm looking for feedback on include ease of debugging, performance, maintainability etc. public static double GetDouble(object input, double defaultVal) { try { return Convert.ToDouble(input); } catch { return defaultVal; } } public static double GetDouble(object input, double defaultVal) { double returnVal; if (double.TryParse(input.ToString(

int.TryParse vs. other methods for determining if a char contains an int

六眼飞鱼酱① 提交于 2020-01-05 02:27:38
问题 When using the char datatype is there any reason one should use int.TryParse int.TryParse(inputChar.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out curNum) vs. inputChar - '0' And checking if the result is between 0-9 ? 回答1: If you want to check if a char is a digit you should use Char.IsDigit: if (Char.IsDigit(inputChar)) { // ... } 回答2: Well, two reasons why I would always use TryParse Using a well-tested library function is always better than re-inventing the wheel. The

int.TryParse vs. other methods for determining if a char contains an int

别来无恙 提交于 2020-01-05 02:27:33
问题 When using the char datatype is there any reason one should use int.TryParse int.TryParse(inputChar.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out curNum) vs. inputChar - '0' And checking if the result is between 0-9 ? 回答1: If you want to check if a char is a digit you should use Char.IsDigit: if (Char.IsDigit(inputChar)) { // ... } 回答2: Well, two reasons why I would always use TryParse Using a well-tested library function is always better than re-inventing the wheel. The

Regex for matching multiple date formats

我与影子孤独终老i 提交于 2020-01-03 18:43:44
问题 What should be the regex for matching date of any format like: 26FEB2009 31DEC2009 27 Mar 2008 30 Jul 2009 26-Feb-2009 27-Aug-2009 29/05/2008 07.11.2008 Jan 11 2008 May 26 2008 What should be the regular expression for that? I have regex that matches with 26-Feb-2009 and 26 FEB 2009 with but not with 26FEB2009 . So if any one know then please update it. (?:^|[^\d\w:])(?'day'\d{1,2})(?:-?st\s+|-?th\s+|-?rd\s+|-?nd\s+|-|\s+)(?'month'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)

Regex for matching multiple date formats

时间秒杀一切 提交于 2020-01-03 18:43:07
问题 What should be the regex for matching date of any format like: 26FEB2009 31DEC2009 27 Mar 2008 30 Jul 2009 26-Feb-2009 27-Aug-2009 29/05/2008 07.11.2008 Jan 11 2008 May 26 2008 What should be the regular expression for that? I have regex that matches with 26-Feb-2009 and 26 FEB 2009 with but not with 26FEB2009 . So if any one know then please update it. (?:^|[^\d\w:])(?'day'\d{1,2})(?:-?st\s+|-?th\s+|-?rd\s+|-?nd\s+|-|\s+)(?'month'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)

Decimal.TryParse doesn't parse my decimal value

被刻印的时光 ゝ 提交于 2020-01-01 07:57:38
问题 When I tried to convert something like 0.1 (from user in textbox), My value b is always false. bool b = Decimal.TryParse("0.1", out value); How can it be here to work? 回答1: Too late to the party, but I was going to suggest forcing the culuture to en-US but Invariant is a better sln decimal value; bool b = Decimal.TryParse("0.1", NumberStyles.Any, new CultureInfo("en-US"), out value); 回答2: Specify the culture for the parsing. Your current culture uses some different number format, probably 0,1