<?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[cout - Nachkommastellen angeben?]]></title><description><![CDATA[<p>Hallo,<br />
ich hab hier glaub ich ne Anfängerfrage bei der ich aber leider gerade nicht weiterkomme.</p>
<p>Ich möchte, zu Übungszwecken, einen Rundungsalgorithmus selber schreiben. Ist auch soweit kein Problem.<br />
Nur wie kann ich die Anzahl der Nachkommastellen mit cout angeben? Am besten noch dynamisch mit ner Variablen? Das Check ich grad nicht. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Danke für die Hilfe<br />
geneticZ</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/246378/cout-nachkommastellen-angeben</link><generator>RSS for Node</generator><lastBuildDate>Sun, 05 Apr 2026 15:40:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/246378.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 27 Jul 2009 12:45:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to cout - Nachkommastellen angeben? on Mon, 27 Jul 2009 12:45:19 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich hab hier glaub ich ne Anfängerfrage bei der ich aber leider gerade nicht weiterkomme.</p>
<p>Ich möchte, zu Übungszwecken, einen Rundungsalgorithmus selber schreiben. Ist auch soweit kein Problem.<br />
Nur wie kann ich die Anzahl der Nachkommastellen mit cout angeben? Am besten noch dynamisch mit ner Variablen? Das Check ich grad nicht. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Danke für die Hilfe<br />
geneticZ</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1750257</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750257</guid><dc:creator><![CDATA[geneticZ]]></dc:creator><pubDate>Mon, 27 Jul 2009 12:45:19 GMT</pubDate></item><item><title><![CDATA[Reply to cout - Nachkommastellen angeben? on Mon, 27 Jul 2009 12:46:45 GMT]]></title><description><![CDATA[<p><code>std::cout.precision(X);</code></p>
<p>siehe auch<br />
<a href="http://www.cplusplus.com/reference/iostream/ios_base/precision/" rel="nofollow">http://www.cplusplus.com/reference/iostream/ios_base/precision/</a></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1750259</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750259</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 27 Jul 2009 12:46:45 GMT</pubDate></item><item><title><![CDATA[Reply to cout - Nachkommastellen angeben? on Mon, 27 Jul 2009 12:52:43 GMT]]></title><description><![CDATA[<p>ja danke, das kannte ich schon habs nicht verwendet weils eben rundet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1750263</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750263</guid><dc:creator><![CDATA[geneticZ]]></dc:creator><pubDate>Mon, 27 Jul 2009 12:52:43 GMT</pubDate></item><item><title><![CDATA[Reply to cout - Nachkommastellen angeben? on Mon, 27 Jul 2009 13:11:11 GMT]]></title><description><![CDATA[<p>geneticZ schrieb:</p>
<blockquote>
<p>ja danke, das kannte ich schon habs nicht verwendet weils eben rundet.</p>
</blockquote>
<p>klar rundet es, so lang du dort nicht die maximale länge für den jeweiligen typen angibst(float/double/long double)<br />
wenn du ne <code>1.999999999999999999999999</code> ausgeben willst, aber nur 5 dezimalstellen anzeigen lassen willst, soll er dann <code>1.99999</code> ausgeben? ich glaube nicht...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1750279</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750279</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 27 Jul 2009 13:11:11 GMT</pubDate></item><item><title><![CDATA[Reply to cout - Nachkommastellen angeben? on Mon, 27 Jul 2009 13:35:22 GMT]]></title><description><![CDATA[<p>Also wenn du was ungerundetes willst, kann man so einen Ansatz machen:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include&lt;sstream&gt;

using namespace std;

template&lt;typename T&gt; class precise;

template&lt;typename T&gt; ostream&amp; operator&lt;&lt;(ostream&amp; stream,precise&lt;T&gt; obj){
  stringstream s;
  s&lt;&lt;obj.data;
  stream&lt;&lt;s.str().substr(0,obj.num)&lt;&lt;endl;
  return stream;
};

template&lt;typename T&gt; class precise{
private:
  int num;
  T data;
public: 
  precise(int i, T t): num(i), data(t) {};
  friend ostream&amp; operator&lt;&lt; &lt;&gt;(ostream&amp; stream, precise obj);
};

int main(){
  cout&lt;&lt;precise&lt;double&gt;(5, 1.2345678);
}
</code></pre>
<p>Geht für alle Typen die einen stringstream Operator &lt;&lt; haben. Man kann noch eine Menge verbessern, da ich es gerade schnell hingeschrieben habe ohne auf Feinheiten zu achten: Const correctness nicht beachtet, keine Referenzen für data verwendet, gibt wirklich exakt die Anzahl Zeichen aus, egal ob es sich um Vor-/Nachkommastellen oder gar das Komma selbst handelt. Die Idee sollte aber klar geworden sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1750302</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750302</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 27 Jul 2009 13:35:22 GMT</pubDate></item><item><title><![CDATA[Reply to cout - Nachkommastellen angeben? on Mon, 27 Jul 2009 14:15:47 GMT]]></title><description><![CDATA[<p>geht so leider nicht, da std::stringstream auch wieder ein precision hat...<br />
Mein Ansatz (nach deiner Idee ^^) wäre so etwas in der Richtung:</p>
<pre><code class="language-cpp">#include &lt;cmath&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

template&lt;typename T&gt;
class precise
{
private:
	int num;
	T data;

	static void set_prec_helper(std::stringstream &amp;ss, int mantisse)
	{
		double tmp = std::pow(2., double(mantisse));
		int prec = int(std::log(tmp));
		ss.precision(prec);
	}

	static void set_prec(std::stringstream &amp;ss)
	{
		set_prec_helper(ss, 0);
	}
public:
	precise(int i): num(i) {}

	precise&amp; operator() (const T &amp;_data)
	{
		data = _data;
		return *this;
	}

	friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp; stream, const precise &amp;obj)
	{
		std::stringstream s;
		obj.set_prec(s);
		s &lt;&lt; obj.data;
		std::string tmp = s.str();
		std::string::size_type len_to_point = tmp.find('.');
		if(len_to_point != std::string::npos)
		{
			std::string::size_type len = len_to_point+2+obj.num;
			if(len &lt; tmp.size())
				tmp.erase(tmp.begin()+len, tmp.end());
		}
		return stream &lt;&lt; tmp;
	}
}; 

template &lt;&gt;
void precise&lt;double&gt;::set_prec(std::stringstream &amp;ss)
{
	set_prec_helper(ss, 52);
}

template &lt;&gt;
void precise&lt;float&gt;::set_prec(std::stringstream &amp;ss)
{
	set_prec_helper(ss, 23);
}

int main()
{
	precise&lt;double&gt; not_rounded(10);
	std::cout &lt;&lt; not_rounded(1.2345678) &lt;&lt; std::endl;
	std::cout &lt;&lt; not_rounded(1.9999999) &lt;&lt; std::endl;
	std::cout &lt;&lt; not_rounded(1.00001) &lt;&lt; std::endl;
	std::cout &lt;&lt; not_rounded(1) &lt;&lt; std::endl;

	precise&lt;int&gt; not_rounded2(1); //template-fähig sollte es ja schon sein...
	std::cout &lt;&lt; not_rounded2(1234) &lt;&lt; std::endl;

	system(&quot;PAUSE&quot;);
}
</code></pre>
<p>Allerdings ist es jz eben auch nicht ungerundet - nur so, wie es intern gespeichert ist...<br />
Ne Traumlösung ist es also auf gar keinen Fall...<br />
Wenn du es genau haben möchtest, musst du halt mit ganzen Zahlen rechnen und dann das Komma noch iwo einfügen...<br />
Was mir auch gar nicht gefällt, ist die Funktion <code>set_prec_helper</code> ... Das ständige rumgecaste ist echt lästig - man könnte es auch mit nem <code>unsigned long long</code> und dem shift-operator implementieren und dann halt mit <code>/= 10</code> war mir aber zu viel gefrickel und man würde sich auf <code>sizeof(long long)</code> als mantissenlänge beschränken - der Typ (den es genau genommen gar nicht gibt), ist zwar mit 64bit lang genug für double und float, aber schon bei long double hört es auf, wenn der nicht gerade so wie im msvc implementiert ist...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1750322</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750322</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 27 Jul 2009 14:15:47 GMT</pubDate></item></channel></rss>