<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[floats mit std::ostream ganz normal formatieren]]></title><description><![CDATA[<p>Ich versuche gerade Fließkommazahlen mit <code>ostream</code> ohne wissenschaftliche Notation zu formatieren und ohne unnötige Nullen am Ende. Klingt komisch, ist aber so.</p>
<pre><code class="language-cpp">#include &lt;sstream&gt;
#include &lt;iostream&gt;

namespace
{
	template &lt;class Float&gt;
	std::string float_to_sane_string(Float value)
	{
		std::ostringstream buffer;
		//gesucht: Optionen für vernünftige Formatierung
		buffer &lt;&lt; value;
		return buffer.str();
	}

	void check_sanity(double value, std::string const &amp;expected)
	{
		std::string const formatted = float_to_sane_string(value);
		if (formatted != expected)
		{
			std::cerr &lt;&lt; &quot;Expected &quot; &lt;&lt; expected &lt;&lt; &quot;, got &quot; &lt;&lt; formatted &lt;&lt; '\n';
		}
	}
}

int main()
{
	check_sanity(0.0, &quot;0&quot;);
	check_sanity(0.5, &quot;0.5&quot;);
	check_sanity(0.00005, &quot;0.00005&quot;);
	check_sanity(1000000000.0, &quot;1000000000&quot;);
}
</code></pre>
<pre><code>Expected 0.00005, got 5e-05
Expected 1000000000, got 1e+09
</code></pre>
<p><code>fixed</code> deaktiviert zwar die wissenschaftliche Notation, produziert aber zu viele Nullen am Ende:</p>
<pre><code class="language-cpp">buffer &lt;&lt; std::fixed &lt;&lt; value;
</code></pre>
<pre><code>Expected 0, got 0.000000
Expected 0.5, got 0.500000
Expected 0.00005, got 0.000050
Expected 1000000000, got 1000000000.000000
</code></pre>
<p>Weiß jemand wie das geht, was ich will?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/325694/floats-mit-std-ostream-ganz-normal-formatieren</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 21:52:33 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325694.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 13 May 2014 20:48:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to floats mit std::ostream ganz normal formatieren on Tue, 13 May 2014 20:48:23 GMT]]></title><description><![CDATA[<p>Ich versuche gerade Fließkommazahlen mit <code>ostream</code> ohne wissenschaftliche Notation zu formatieren und ohne unnötige Nullen am Ende. Klingt komisch, ist aber so.</p>
<pre><code class="language-cpp">#include &lt;sstream&gt;
#include &lt;iostream&gt;

namespace
{
	template &lt;class Float&gt;
	std::string float_to_sane_string(Float value)
	{
		std::ostringstream buffer;
		//gesucht: Optionen für vernünftige Formatierung
		buffer &lt;&lt; value;
		return buffer.str();
	}

	void check_sanity(double value, std::string const &amp;expected)
	{
		std::string const formatted = float_to_sane_string(value);
		if (formatted != expected)
		{
			std::cerr &lt;&lt; &quot;Expected &quot; &lt;&lt; expected &lt;&lt; &quot;, got &quot; &lt;&lt; formatted &lt;&lt; '\n';
		}
	}
}

int main()
{
	check_sanity(0.0, &quot;0&quot;);
	check_sanity(0.5, &quot;0.5&quot;);
	check_sanity(0.00005, &quot;0.00005&quot;);
	check_sanity(1000000000.0, &quot;1000000000&quot;);
}
</code></pre>
<pre><code>Expected 0.00005, got 5e-05
Expected 1000000000, got 1e+09
</code></pre>
<p><code>fixed</code> deaktiviert zwar die wissenschaftliche Notation, produziert aber zu viele Nullen am Ende:</p>
<pre><code class="language-cpp">buffer &lt;&lt; std::fixed &lt;&lt; value;
</code></pre>
<pre><code>Expected 0, got 0.000000
Expected 0.5, got 0.500000
Expected 0.00005, got 0.000050
Expected 1000000000, got 1000000000.000000
</code></pre>
<p>Weiß jemand wie das geht, was ich will?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399061</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399061</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Tue, 13 May 2014 20:48:23 GMT</pubDate></item><item><title><![CDATA[Reply to floats mit std::ostream ganz normal formatieren on Tue, 13 May 2014 21:01:54 GMT]]></title><description><![CDATA[<p>Ich verstehe nicht, wieso du die iostreams verwenden willst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399065</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399065</guid><dc:creator><![CDATA[unverständlich]]></dc:creator><pubDate>Tue, 13 May 2014 21:01:54 GMT</pubDate></item><item><title><![CDATA[Reply to floats mit std::ostream ganz normal formatieren on Tue, 13 May 2014 21:22:14 GMT]]></title><description><![CDATA[<pre><code>template &lt;class Float&gt;
	std::string float_to_sane_string(Float value)
	{
		auto str = std::to_string(value);
		boost::algorithm::trim_right_if( str, [](char c){ return c == '0'; } );
		return str;
	}
</code></pre>
<p>Darauf hättest du aber auch locker selber kommen können. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Edit: <code>to_string</code> generiert übrigens nie die Exponentialnotation, weil <code>to_string</code> mit einer Fließkommazahl als Argument <code>sprintf</code> mit conversion-specifier <code>f</code> bzw. <code>Lf</code> aufruft, welche nur Dezimalnotation generieren.<br />
Ob man das Anhängen der Nullen auch in den C++ IOStreams unterbinden kann, weiß ich nicht. Ist das in irgendeiner Weise performance-kritisch? Trimmen ist doch fix genug und leicht lesbar?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399067</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399067</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 13 May 2014 21:22:14 GMT</pubDate></item><item><title><![CDATA[Reply to floats mit std::ostream ganz normal formatieren on Tue, 13 May 2014 21:22:17 GMT]]></title><description><![CDATA[<p>unverständlich schrieb:</p>
<blockquote>
<p>Ich verstehe nicht, wieso du die iostreams verwenden willst.</p>
</blockquote>
<p>Dann zeig mal, wie du das mit printf hin bekommst. Da geht das nämlich genau so wenig.</p>
<p>In dem Satz ist übrigens auch die Antwort für den Threadersteller versteckt: Es geht nicht. Zumindest nicht mit den Standardflags. Die kanonischen Wege, dies zu erreichen sind:<br />
-Stringmanipulation: Erst lang in einen String ausgeben, dann die Nullen abschneiden. Das kann man auch schick als Funktion schreiben, so dass man etwas wie <code>cout &lt;&lt; my_format(value)</code> schreiben kann.<br />
-Tief in die IOStreams eintauchen, und sich eine passende num_put-Facet schreiben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399069</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 13 May 2014 21:22:17 GMT</pubDate></item><item><title><![CDATA[Reply to floats mit std::ostream ganz normal formatieren on Tue, 13 May 2014 21:44:08 GMT]]></title><description><![CDATA[<p>Hab das just 4 fun mal händisch gemacht:</p>
<pre><code>#include &lt;sstream&gt;
#include &lt;iostream&gt;
#include &lt;cassert&gt;
#include &lt;cmath&gt;

namespace
{
    template &lt;class Float&gt;
    std::string float_to_sane_string(Float value)
    {
        typedef unsigned long long Int;
        std::ostringstream buffer;

        if(value &lt; 0) buffer &lt;&lt; '-';
        value = std::fabs(value);

        buffer &lt;&lt; Int(value);
        value -= Int(value);
        assert(value &lt; 1);
        if(value == 0)
            return buffer.str();

        buffer &lt;&lt; '.';
        do
        {
            value *= 10;
            Float newValue = value - unsigned(value);
            if(newValue &gt; 0.99)
            {
                buffer &lt;&lt; (unsigned(value) + 1);
                break;
            }

            buffer &lt;&lt; unsigned(value);
            value = newValue;
            assert(value &lt; 1);
        } while(value != 0);

        return buffer.str();
    }

    void check_sanity(double value, std::string const &amp;expected)
    {
        std::string const formatted = float_to_sane_string(value);
        if (formatted != expected)
        {
            std::cerr &lt;&lt; &quot;Expected &quot; &lt;&lt; expected &lt;&lt; &quot;, got &quot; &lt;&lt; formatted &lt;&lt; '\n';
        }
    }
}

int main()
{
    check_sanity(0.0, &quot;0&quot;);
    check_sanity(0.5, &quot;0.5&quot;);
    check_sanity(0.00005, &quot;0.00005&quot;);
    check_sanity(1000000000.0, &quot;1000000000&quot;);
}
</code></pre>
<p>IEEE-754 Geeks: Irgendetwas daran falsch mit der Ausnahme nan etc?<br />
Ergebnisse sehen recht brauchbar aus bei meinen Tests.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399070</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399070</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Tue, 13 May 2014 21:44:08 GMT</pubDate></item><item><title><![CDATA[Reply to floats mit std::ostream ganz normal formatieren on Tue, 13 May 2014 21:45:13 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<pre><code>template &lt;class Float&gt;
	std::string float_to_sane_string(Float value)
	{
		auto str = std::to_string(value);
		boost::algorithm::trim_right_if( str, [](char c){ return c == '0'; } );
		return str;
	}
</code></pre>
<p>Darauf hättest du aber auch locker selber kommen können. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Edit: <code>to_string</code> generiert übrigens nie die Exponentialnotation, weil <code>to_string</code> mit einer Fließkommazahl als Argument <code>sprintf</code> mit conversion-specifier <code>f</code> bzw. <code>Lf</code> aufruft, welche nur Dezimalnotation generieren.<br />
Ob man das Anhängen der Nullen auch in den C++ IOStreams unterbinden kann, weiß ich nicht. Ist das in irgendeiner Weise performance-kritisch? Trimmen ist doch fix genug und leicht lesbar?</p>
</blockquote>
<p>Das kommt nicht durch seine Tests da er ja erwartet dass 5.0 zu 5 wird, deine Lösung macht 5. daraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399071</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399071</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Tue, 13 May 2014 21:45:13 GMT</pubDate></item><item><title><![CDATA[Reply to floats mit std::ostream ganz normal formatieren on Tue, 13 May 2014 21:53:25 GMT]]></title><description><![CDATA[<blockquote>
<p>Das kommt nicht durch seine Tests da er ja erwartet dass 5.0 zu 5 wird, deine Lösung macht 5. daraus.</p>
</blockquote>
<p>Das macht es kein bisschen komplizierter</p>
<pre><code>template &lt;class Float&gt;
	std::string float_to_sane_string(Float value)
	{
		auto str = std::to_string(value);
		boost::algorithm::trim_right_if( str, [](char c){ return c == '0'; } );
		if( !str.empty() &amp;&amp; str.back() == '.' )
			str.pop_back();
		return str;
	}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2399073</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399073</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 13 May 2014 21:53:25 GMT</pubDate></item><item><title><![CDATA[Reply to floats mit std::ostream ganz normal formatieren on Tue, 13 May 2014 21:55:26 GMT]]></title><description><![CDATA[<p>Na klar, wollte es nur anmerken da er ja freundlicherweise sogar Testfälle geliefert hat. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399076</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399076</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Tue, 13 May 2014 21:55:26 GMT</pubDate></item><item><title><![CDATA[Reply to floats mit std::ostream ganz normal formatieren on Tue, 13 May 2014 22:08:18 GMT]]></title><description><![CDATA[<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399077</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399077</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Tue, 13 May 2014 22:08:18 GMT</pubDate></item><item><title><![CDATA[Reply to floats mit std::ostream ganz normal formatieren on Wed, 14 May 2014 17:52:49 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Es geht nicht. Zumindest nicht mit den Standardflags. Die kanonischen Wege, dies zu erreichen sind:<br />
-Stringmanipulation: Erst lang in einen String ausgeben, dann die Nullen abschneiden. Das kann man auch schick als Funktion schreiben, so dass man etwas wie <code>cout &lt;&lt; my_format(value)</code> schreiben kann.</p>
</blockquote>
<p>Danke für die Auskunft. Ich mache das jetzt mit einem lokalen <code>ostringstream</code> und Abschneiden.<br />
Das muss auch gar nicht schnell sein. Ich wollte nur die Float-Formatierung nicht neu erfinden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2399191</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2399191</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Wed, 14 May 2014 17:52:49 GMT</pubDate></item></channel></rss>