<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>atof considers locale under linux?</title>
		<link>http://www.allegro.cc/forums/view/600366</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 25 May 2009 01:17:17 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I recently tested my game under linux just to see that it doesn&#39;t work correctly.<br />I hunted the problem down to some float values being 0 which should not be.<br />All those values are converted to float from string using atof().</p><p>The problem now is that atof under linux seems to take the locale of my machine into account.</p><p>I&#39;m on a german windows xp so normally float values are written like 0,5 not like 0.5.</p><p>This code works under windows: 
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">char</span> <span class="k3">*</span> c <span class="k3">=</span> <span class="s">"0.5"</span><span class="k2">;</span>
<span class="k1">float</span> f <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_53.html" target="_blank">atof</a><span class="k2">(</span>c<span class="k2">)</span><span class="k2">;</span> <span class="c">// f = 0.5</span>
</pre></div></div><p>

But on linux it behaves like this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">char</span> <span class="k3">*</span> c1 <span class="k3">=</span> <span class="s">"0.5"</span><span class="k2">;</span>
<span class="k1">char</span> <span class="k3">*</span> c2 <span class="k3">=</span> <span class="s">"0,5"</span><span class="k2">;</span>
<span class="k1">float</span> f1 <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_53.html" target="_blank">atof</a><span class="k2">(</span>c1<span class="k2">)</span><span class="k2">;</span> <span class="c">// f1 = 0</span>
<span class="k1">float</span> f2 <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_53.html" target="_blank">atof</a><span class="k2">(</span>c2<span class="k2">)</span><span class="k2">;</span> <span class="c">// f2 = 0.5</span>
</pre></div></div><p>

What did I miss?<br />Why is there the behavior different? And how should I deal with this problem?<br />I&#39;m very surprised that atof is locale aware! <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (count)</author>
		<pubDate>Sun, 24 May 2009 23:01:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It seems atof and strtod are locale dependant. You&#39;ll probably want to set the locale to C in your program.</p><p><span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_698.html" target="_blank">setenv</a><span class="k2">(</span><span class="s">"LC_ALL"</span>, <span class="s">"C"</span><span class="k2">)</span><span class="k2">;</span></span> might be all you need. Or if you only want to change the numerics: <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_698.html" target="_blank">setenv</a><span class="k2">(</span><span class="s">"LC_NUMERIC"</span>, <span class="s">"C"</span><span class="k2">)</span><span class="k2">;</span></span></p><p>editInf:</p><p>libc might not notice env vars in a running program, I don&#39;t know, if it doesn&#39;t try: </p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;locale.h&gt;</span>
<a href="http://www.delorie.com/djgpp/doc/libc/libc_705.html" target="_blank">setlocale</a><span class="k2">(</span>LC_NUMERIC, <span class="s">"C"</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 24 May 2009 23:02:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>strtod behaves exactly the same.<br />So i will check out how to set the locale. Never did so with c++.<br />Did I understand you correctly that &quot;C&quot; is an own locale?</p><p>EDIT:<br />Missed your edit <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />Thanks for the infos! Will try that.</p><p>EDIT2:<br />But why is it different for windows and linux?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (count)</author>
		<pubDate>Sun, 24 May 2009 23:13:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/600366/813292#target">Christopher Bludau</a> said:</div><div class="quote"><p>Did I understand you correctly that &quot;C&quot; is an own locale?</p></div></div><p>Yup. Its a simplified locale that libc uses when nothing else is set. Its also the default fairly often if you don&#39;t select one on install.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>But why is it different for windows and linux?</p></div></div><p>Probably because windows doesn&#39;t support &quot;Locales&quot;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 24 May 2009 23:17:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/600366/813293#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p>
Probably because windows doesn&#39;t support &quot;Locales&quot;.
</p></div></div><p>
Ok. Sounds reasonable. </p><p>It is working now.<br />Thank you very much! <br />(Forgot to make this a cookie thread <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (count)</author>
		<pubDate>Sun, 24 May 2009 23:35:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Windows does support locales. We were bitten by this problem when some code that used sscanf() under MSVC stopped working mysteriously.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Sun, 24 May 2009 23:55:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/600366/813298#target">X-G</a> said:</div><div class="quote"><p>Windows does support locales.</p></div></div><p>Standard libc locale&#39;s? or its own notion?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 24 May 2009 23:58:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>How did you solve it?<br />Like Thomas suggested?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (count)</author>
		<pubDate>Sun, 24 May 2009 23:58:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/600366/813300#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p>Standard libc locale&#39;s? or its own notion? </p></div></div><p>

Standard libc locales, yes.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/600366/813301#target">Christopher Bludau</a> said:</div><div class="quote"><p>How did you solve it?</p></div></div><p>

setlocale(LC_ALL, &quot;C&quot;); as a hack, fixed it properly by getting rid of sscanf (which is a horrid function anyway).</p><p>(EDIT, Protip: Under C++, you can imbue() an iostream with the locale you want.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Mon, 25 May 2009 01:08:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/600366/813313#target">X-G</a> said:</div><div class="quote"><p>
Protip: Under C++, you can imbue() an iostream with the locale you want.
</p></div></div><p>

Nice! That&#39;s pretty useful.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (count)</author>
		<pubDate>Mon, 25 May 2009 01:17:17 +0000</pubDate>
	</item>
</rss>
