tryparse

Converting exponential number to decimal 1.11111117E+9 - trailing digits become zero

我只是一个虾纸丫 提交于 2019-12-12 21:20:23
问题 I'm trying to convert and exponential number 1.11111117E+9 which is actually a 10 digit number '1111111111'. When I'm trying to convert this exponential number using decimal.TryParse method it is making last 3 digits as zero and giving the number as ' 111111000 '. This is happening with any 10 digit number. decimal amount; decimal.TryParse("1.11111117E+9", NumberStyles.Any, null, out amount); This is weird but I'm not able to figure out what's the issue here, can anybody tell me what's wrong

How to check for empty textbox

*爱你&永不变心* 提交于 2019-12-11 04:06:10
问题 On a site i found the try parse method (how to check if there is a empty textbox in C#) but i don't know how to use it. int outputValue=0; bool isNumber=false; isNumber=int.TryParse(textBox1.Text, out outputValue); if(!isNumber) { MessageBox.Show("Type numbers in the textboxes"); } else { // some code } and how can i solve this for 1+ number of textboxes 回答1: If you want to check empty for all text box control in your page .Try IsNullOrWhiteSpace foreach (Control child in this.Controls) {

TryParse double values

£可爱£侵袭症+ 提交于 2019-12-11 03:59:16
问题 I need validate double values. When I TryParse .00 or ,00 return false , but I allow this values must true. This code not work correctly for this case model = ,00 or .002 or ,789 or .12 ... double number; double.TryParse(model, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out number) 回答1: You could loop all supported cultures: bool parsable = false; double number = 0; var supportedCultures = new[] { CultureInfo.InvariantCulture, CultureInfo.CreateSpecificCulture("de-DE") }; foreach(var

Make TryParse compatible with comma or dot decimal separator

帅比萌擦擦* 提交于 2019-12-10 22:03:18
问题 The problem: Let's assume you are using a dot "." as a decimal separator in your regional setting and have coded a string with a comma. string str = "2,5"; What happens when you decimal.TryParse(str, out somevariable); it? somevariable will assume 0. What can you do to solve it? 1- You can decimal.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out somevariable); And it will return 25, and not 2.5 which is wrong. 2- You can decimal.TryParse(str.Replace(",","."), out num); And it

Is there a GUID.TryParse() in .NET 3.5?

五迷三道 提交于 2019-12-09 02:24:44
问题 UPDATE Guid.TryParse is available in .NET 4.0 END UPDATE Obviously there is no public GUID.TryParse() in .NET CLR 2.0. So, I was looking into regular expressions [aka googling around to find one] and each time I found one there was a heated argument in the comments section about RegEx A doesn't work, use RegEx B. Then someone would write Regex C yadda yadda So anyway, What I decided to do was this, but I feel bad about it. public static bool IsGuid (string possibleGuid) { try { Guid gid = new

DateTime.TryParseExact not working as expected

孤街醉人 提交于 2019-12-06 19:42:18
问题 Can anyone explain why the following snippet returns true? According to the docs for The "d" custom format specifier, "A single-digit day is formatted without a leading zero." So why doesn't TryParseExact fail when I give it a single-digit day with a leading zero? DateTime x; return DateTime.TryParseExact ( "01/01/2001", @"d\/MM\/yyyy", null, System.Globalization.DateTimeStyles.None, out x ); UPDATE I think maybe I was unclear originally. What I am really trying to get at is: Why does

Convert a string in a List<int> using LINQ (cleaner way)

好久不见. 提交于 2019-12-06 15:56:44
I have this string: string input = "1,2,3,4,s,6"; Pay attention to the s character. I just want to convert this string in a List<int> using LINQ. I initially tried in this way: var myList = new List<int>(); input.Split(',').ToList().ForEach(n => myList.Add(int.TryParse(n, out int num) ? num : -1) ); lista.RemoveAll(e => e == -1); But I prefer not have any -1 instead of a no-number characters. So now I try with this: var myList = new List<int>(); input.Split(',').ToList() .FindAll(n => int.TryParse(n, out int _)) .ForEach(num => myList.Add(int.Parse(num))); I prefer this, but is really a shame

pros and cons of TryCatch versus TryParse

前提是你 提交于 2019-12-05 02:18:41
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(), out returnVal)) { return returnVal; } else { return defaultVal; } } TryParse will be faster than

DateTime.TryParseExact not working as expected

烈酒焚心 提交于 2019-12-05 01:08:34
Can anyone explain why the following snippet returns true? According to the docs for The "d" custom format specifier , "A single-digit day is formatted without a leading zero." So why doesn't TryParseExact fail when I give it a single-digit day with a leading zero? DateTime x; return DateTime.TryParseExact ( "01/01/2001", @"d\/MM\/yyyy", null, System.Globalization.DateTimeStyles.None, out x ); UPDATE I think maybe I was unclear originally. What I am really trying to get at is: Why does TryParseExact accept some values that don't match exactly? from all of the documentation I have seen, 'd'

Regex vs Tryparse what is the best in performance

前提是你 提交于 2019-12-04 03:11:46
问题 In my ASP.net project I need to validate some basic data types for user inputs. The data types are like numeric, decimal, datetime etc. What is the best approach that I should have taken in terms of performance? Is it to do it by Regex.IsMatch() or by TryParse() ? Thanks in advance. 回答1: As other would say, the best way to answer that is to measure it ;) static void Main(string[] args) { List<double> meansFailedTryParse = new List<double>(); List<double> meansFailedRegEx = new List<double>();