This question already has answers here:
Identify if a string is a number
(26 answers)
Regex for numbers only
(20 answers)
Closed 8 years ago.
How to check the string is number or not. I am verifying mobile number codes in which it should have 10 digits and only in numerical format.
string str="9848768447"
if(str.Length==10 && Here I need condition to check string is number or not)
{
//Code goes here
}
I am new to programming. please help me
Use int.TryParse:
int i;
if(str.Length==10 && int.TryParse(str, out i))
{
//Code goes here
}
Another way which has issues with unicode digits is using Char.IsDigit:
if(str.Length==10 && str.All(Char.IsDigit))
{
}
Related
This question already has answers here:
How to convert string to double with proper cultureinfo
(7 answers)
Closed 3 years ago.
I'm reading .txt file and spliting values into array like this:
for (int i = 0; i < articleItems.Length; i++)
{
List<string> splitStrings = articleItems[i].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToList();
splitStrings[0] = splitStrings[0].Substring(12);
}
After that I'm trying to make object from those values, and everything is fine, but strange thing are happening here...
Here is the image from debugger, there is 14.80 at the beginning and when I convert that string to decimal it becomes 1480...:
Try
Double.Parse(splitStrings[2].ToString())
This question already has answers here:
C# Getting strange results for a simple math operations
(4 answers)
Is floating point math broken?
(31 answers)
Closed 5 years ago.
Why (0.406 * 10000.0) returns 4060.0000000000005 instead of 4060.0 in C#
I have written a function which checks no. of decimals in a double value and below is the code I am using. The problem described in the above sentence occurs when value of d is 0.406 and values of n is 4 and the function returns true instead of false
I am open to using alternate solution.
public static bool HasMoreThanNDecimals(double d, int n)
{
return !(d * (double)Math.Pow(10, n) % 1 == 0);
}
Just use decimal type instead of double for more precision to get the desired result.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/921a8ffc-9829-4145-bdc9-a96c1ec174a5/decimal-vs-double-difference?forum=csharpgeneral
This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 2 years ago.
I would like to use a regex for a textbox that allows only numbers between 1-5000
I've tried the following but it wont work:
#"/^(?:1|5000|-[1-9]\d?)$/
You can use ^(?:[1-9]|\d{2,3}|[1-4]\d{3}|5000)$. But you'd better to parse to Int then do simple maths.
With some parsing beforehand, you can make the regex very simple:
string s = textBox1.Text;
string r = "";
int n = 0;
if (int.TryParse(s, out n) && (n>=1 && n<=5000))
{
r = "y";
}
if (Regex.IsMatch(r, "y")) {
// input was valid
MessageBox.Show("OK");
}
Try ...
^(?:[1-4][0-9]{1,3}|[1-9][0-9]{0,2}|5000)$
You can do something like the following:
^(([1-4][0-9]{0,3})|([1-9][0-9]{0,2})|(5000))$
The first two groups will match anything in the range of 1 - 4999. You add the |5000 at the end to make it match the range 1 - 5000. The three cases here are:
The number is exactly 5000
The number is between 1 and 3 digits. In this case, it can't possibly be more than 5000. However, the first number must be 1 - 9 so that you can't get something like "009" or "000."
The number is 4 digits, in which case it must be between 1000 - 4999
With that said, I think it would probably be simpler to just parse the int and see if it's in range.
You can try something like this (0-366)
^(0?[0-9]?[0-9]|[1-2][0-9][0-9]|3[0-5][0-9]|36[0-6])$
This question already has answers here:
How to split a number into individual digits in c#? [duplicate]
(6 answers)
Closed 8 years ago.
I have a long number that I want to convert to a list of integers corresponding to decimal digits.
long l = 9876543210L;
List<int> list = // how?
Expecetd result: [0,1,2,3,4,5,6,7,8,9] so list[0] will be 9, list[1] will be 8 (2nd digit from the left), etc.
If you're happy to do this via string processing, you could use:
long l = ...;
var list = l.ToString().Select(c => int.Parse(c.ToString())).ToList();
This converts the number to a string (in decimal), then parses each character in the string as an integer.
If performance is critical, you're better off doing this numerically.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there an easy way in .NET to get “st”, “nd”, “rd” and “th” endings for numbers?
Is there anything already built into C# that formats a number as a ranking position?
By "ranking position" is mean one of ["st","nd,"rd","th"] eg : (1st, 2nd, 3rd, 4th).
I know it'd be easy to write an extension for this, I'm just wondering if there is already something in the language to cater.
Cheers
No, there is nothing built-in for this purpose. Not sure if its relevant, but consider cultural differences if you span languages/cultures.
I've been corrected. Please see the duplicate questions as described in the comments above.
Not out-of-the-box; however, as you mentioned, an extension is pretty easy.
public static string ConvertToRankingPosition(this int value)
{
var digit = value % 10;
return value != 11 && digit == 1 : value + "st" :
value != 12 && digit == 2 : value + "nd" :
value != 13 && digit == 3 : value + "rd" :
value + "th"
}