Why double.TryParse("0.0000", out doubleValue) returns false ?
it takes the localization settings at runtime into account... perhaps you are running this on a system where .
is not the decimal point but ,
instead...
In your specific case I assume you want a fixed culture regardless of the system you are running on with .
as the decimal point:
double.TryParse("0.0000", NumberStyles.Number, CultureInfo.CreateSpecificCulture ("en-US"), out temp)
OR
double.TryParse("0.0000", NumberStyles.Number,CultureInfo.InvariantCulture, out temp)
Some MSDN reference links:
- Double.TryParse(String, NumberStyles, IFormatProvider, Double)
- CultureInfo.CreateSpecificCulture(String)
- IFormatProvider Interface
- CultureInfo.InvariantCulture Property
TryParse
uses the current culture by default. And if your current culture uses a decimal seperator different from .
, it can't parse 0.0000
as you intend. So you need to pass in CultureInfo.InvariantCulture
.
var numberStyle = NumberStyles.AllowLeadingWhite |
NumberStyles.AllowTrailingWhite |
NumberStyles.AllowLeadingSign |
NumberStyles.AllowDecimalPoint |
NumberStyles.AllowThousands |
NumberStyles.AllowExponent;//Choose what you need
double.TryParse( "0.0000", numberStyle, CultureInfo.InvariantCulture, out myVar)
Almost certainly the problem is that Thread.CurrentCulture
does not use dot as the decimal separator.
If you know that the number will be always formatted with dot as the decimal separator, use this code that utilizes the other overload of double.TryParse
:
style = NumberStyles.Float | NumberStyles.AllowThousands;
culture = CultureInfo.InvariantCulture;
float num;
if (double.TryParse("0.0000", style, culture, out num)) {
// whatever
}