Why does System.Convert("0") throw a FormatException on some systems?
The code is compiled in VS2008 targeting .NET3.5. This is not reproducible on my system. I suspect some sort of localization setting is at play but I don't know much about that.
All other valid numbers seem to work fine. The bug is illustrated with this code (which causes the same exception but is not the production code):
using System;
using System.开发者_开发百科Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string str = "";
do
{
str = Console.ReadLine();
Console.WriteLine("\t\"{0}\"", Convert.ToDouble(str));
}
while (str != null);
}
}
}
At the command line, input of "0" crashes the app on at least one system I have encountered.
Stack trace from user's PC:
System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Double.Parse(String s, NumberStyles style, NumberFormatInfo info)
at System.Convert.ToDouble(String value)
I remember this problem from a question a while back. The Parse() methods are affected by the user overrides in the Control Panel + Region and Language applet. IIRC, it is especially sensitive to the "Negative sign symbol" setting. Ask your user to correct the settings there.
The reference question is here.
If your problem is related to current culture, try converting to Double using Invariant Culture:
Convert.ToDouble("0", System.Globalization.CultureInfo.InvariantCulture);
It's quite easy to prove that it's not because of the code(or the CultureInfo), I can prove that for all cultures in .NET, a string "0" can be converted to double correctly.
string inputNumber = "0";
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
try
{
double d = Convert.ToDouble(inputNumber, culture);
}
catch
{
Console.WriteLine(culture.Name);
}
}
Console.WriteLine("end");
Console.Read();
It outputs nothing but an "end".
It might be related to culture settings. As I know in some culture settings you should type in 0.0 to be able to convert to double
I don't think it does crash when you input 0
.
It will, of course, crash when you input anything that is not a number. This means that it will crash if you input an empty string (in other words, just push enter). I imagine this is what you are experiencing.
You code would work (for only numbers) if you change it to this:
string str = "";
do
{
str = Console.ReadLine();
if(!string.IsNullOrEmpty(str))
Console.WriteLine("\t\"{0}\"", Convert.ToDouble(str));
}
while (str != "");
The CurrentCulture CultureInfo instance is probably to blame. Convert.ToDouble call, after all, simply returns the result of Double.Parse. This, as documented, uses the current culture's NumberFormatInfo to figure things out.
精彩评论