<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yet Another [À Compléter] &#187; i18n</title>
	<atom:link href="http://blog.neteril.org/tag/i18n/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.neteril.org</link>
	<description>Random thoughts of Jérémie Laval</description>
	<lastBuildDate>Mon, 19 Jul 2010 17:12:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Beware of double.Parse()</title>
		<link>http://blog.neteril.org/2008/05/28/beware-of-doubleparse/</link>
		<comments>http://blog.neteril.org/2008/05/28/beware-of-doubleparse/#comments</comments>
		<pubDate>Wed, 28 May 2008 11:05:35 +0000</pubDate>
		<dc:creator>Jérémie Laval</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[Monodevelop]]></category>
		<category><![CDATA[double]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[parsing]]></category>

		<guid isPermaLink="false">http://garuma.wordpress.com/?p=187</guid>
		<description><![CDATA[Some svn up ago, Monodevelop started to throw strange exceptions at my face about Format error occuring when parsing a string with double.Parse(). This was triggered in various places ranging from startup, project opening, file opening or application unloading. At the time I hadn&#8217;t bothered tracking down the issue but last Monday I decided to [...]]]></description>
			<content:encoded><![CDATA[<p>Some <code>svn up</code> ago, Monodevelop started to throw strange exceptions at my face about Format error occuring when parsing a string with <code>double.Parse()</code>. This was triggered in various places ranging from startup, project opening, file opening or application unloading. At the time I hadn&#8217;t bothered tracking down the issue but last Monday I decided to investigate it. Turned out that double.Parse was throwing a FormatException about &#8216;.&#8217; (like in &laquo;&nbsp;12.68&#8243;) which was considered unknown.</p>
<p>After poking and modifying some stuff in Double.cs the problem was remaining. So, I made a really simple test case that I pasted below :</p>
<pre class="brush: csharp;">using System;

class DoubleFormatTestCase
{
  public static void Main()
  {
	Console.WriteLine(double.Parse(&quot;12.68&quot;));
  }
}</pre>
<p>Compiling it and running it on my machine threw the same exception that in Monodevelop. I admit this puzzled me completely as I was thinking that, programming language speaking, floating numbers were always represented like this in their textual form.</p>
<p>After asking in #mono, yakeen suggested replacing the simple double.Parse(string) with double.Parse(string, CultureInfo) like this :
<pre class="brush: csharp;">using System;
using System.Globalization;
class DoubleFormatTestCase
{
  public static void Main()
  {
	Console.WriteLine(double.Parse(&quot;12.68&quot;,
                    CultureInfo.InvariantCulture.NumberFormat));
  }
} </pre>
<p>And if you try this code it should work no matter your platform or your language.</p>
<p>Why that ? Because as you should know (and as I should have remembered <img src='http://blog.neteril.org/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  ) .NET/Mono enforces that things like string comparisons, DateTime handling or long.Parse (in our case) are made according to the user culture. Indeed, here in France, when we write floating numbers, we use &#8216;,&#8217; and not &#8216;.&#8217; to separate the integer and floating part. This syntax difference explained why long.Parse(string) was borking on my system.</p>
<p>You can also re-run the first test like this : <code>LANG=en_EN mono DoubleFormatTestCase.exe</code>. Normally it should run without problem.</p>
<p>The moral of the story : always use <code>double.Parse(string, System.Globalization.CultureInfo.InvariantCulture.NumberFormat);</code> instead of <code>double.Parse(string)</code> when doing conversion between string and double in a persistent way (configuration files, intern calculations) and only use the latter when you have to exchange with the user.</p>
<p>Btw, with big kudos to yakeen, I submitted a patch to Monodevelop to correct the problem. It should be available soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.neteril.org/2008/05/28/beware-of-doubleparse/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
