Tags

,

The Problem
Our C# application sent a query to Solr server, and parsing the response. It works fine, until recently one of our customers hits one exception:
Exception Message [Input string was not in a correct format.]
System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Double.Parse(String s, NumberStyles style)

The Root Cause
Check the language settings, the server machine is using Finnish:fi-FI. 
The Solr response always uses en-US locale format for numbers, it uses “.” period as decimal separator.

But in fi-FI[Finnish(Finland)], it uses “,” comma as decimal separator.

This would cause double.parse fail to parse the string like: 10.01.

The fix is easy: using en-US culture when do double.parse: double.Parse(“10.01”, new CultureInfo(“en-US”)); or set en-US culture as the current culture of current thread:
Thread.CurrentThread.CurrentCulture = new CultureInfo(“en-US”);

//<![CDATA[
if(showAdsense){
document.write("(adsbygoogle = window.adsbygoogle || []).push({});”)
}
//]]>

Test Code

static void Main(string[] args)
{
 CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
 Console.WriteLine("Orignal Culture: " + originalCulture);
 CultureInfo fiFi = new CultureInfo("fi-FI");
 Thread.CurrentThread.CurrentCulture = fiFi;

 // print 10,01
 Console.WriteLine(10.01);
 double d1 = double.Parse("10,01");

 // use en-US to parse, this works fine.
 CultureInfo enUS = new CultureInfo("en-US");
 double d2 = double.Parse("10.01", enUS);
 try
 {
   // the follwing code would cause exception:  Input string was not in a correct format.
   double d3 = double.Parse("10.01");
 }
 catch(Exception e)
 {
   Console.WriteLine(e);
 }

 Thread.CurrentThread.CurrentCulture = enUS;
 double d4 = double.Parse("10.01");

 Thread.CurrentThread.CurrentCulture = originalCulture;
 Console.ReadLine();
}


Resources:
Java Locale “Finnish (Finland)” (fi-FI)

via Blogger http://ift.tt/1m6F0CK

Advertisements