I need to check if a string contains only digits. How could I achieve this in C#?
string s = "123" → valid
string s = "123.67" → valid
string s = "123F" → invalid
Is there any function like IsNumeric?
A better regex would be /^\d*\.?\ d+$/ which would force a digit after a decimal point. @Chandranshu and it matches an empty string, which your change would also solve.
To validate decimal numbers in JavaScript, use the match() method. It retrieves the matches when matching a string against a regular expression.
String strDouble = String. format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.
double n;
if (Double.TryParse("128337.812738", out n)) {
// ok
}
works assuming the number doesn't overflow a double
for a huge string, try the regexp:
if (Regex.Match(str, @"^[0-9]+(\.[0-9]+)?$")) {
// ok
}
add in scientific notation (e/E) or +/- signs if needed...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With