<?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[float Anzahl Stellen vor Komma]]></title><description><![CDATA[<p>Hallo,</p>
<p>wie kann man die Anzahl der Stellen vor dem Komma eines floats bekommen? Wie kann man einen float auf n Nachkommastellen runden?</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/233738/float-anzahl-stellen-vor-komma</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 04:21:12 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/233738.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 08 Feb 2009 16:10:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to float Anzahl Stellen vor Komma on Sun, 08 Feb 2009 16:10:37 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>wie kann man die Anzahl der Stellen vor dem Komma eines floats bekommen? Wie kann man einen float auf n Nachkommastellen runden?</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660038</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660038</guid><dc:creator><![CDATA[filth]]></dc:creator><pubDate>Sun, 08 Feb 2009 16:10:37 GMT</pubDate></item><item><title><![CDATA[Reply to float Anzahl Stellen vor Komma on Sun, 08 Feb 2009 16:28:49 GMT]]></title><description><![CDATA[<p>Die Anzahl der Vorkommastellen erhält man mit</p>
<pre><code class="language-cpp">int vk = int( log10( abs( zahl ) ) ) + 1; // erfordert #include &lt;cmath&gt;
</code></pre>
<p>Runden beim Ausgeben von Fließkommazahlen kann man mit den Manipulatoren fixed und setprecision - z.B.:</p>
<pre><code class="language-cpp">double pi = acos( -1.0 ); // erfordert #include &lt;cmath&gt;
    cout &lt;&lt; fixed &lt;&lt; setprecision(4); // 4 Nachkommastellen für alle Fließkommazahlen
    cout &lt;&lt; &quot;Pi = &quot; &lt;&lt; setw(8) &lt;&lt; pi &lt;&lt; endl;
</code></pre>
<p>erfordert #include &lt;iomanip&gt;</p>
<p>Gruß<br />
Werner</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660048</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660048</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Sun, 08 Feb 2009 16:28:49 GMT</pubDate></item><item><title><![CDATA[Reply to float Anzahl Stellen vor Komma on Sun, 08 Feb 2009 16:33:05 GMT]]></title><description><![CDATA[<p>Sowas?</p>
<p>a) 10.1635 -&gt; 2 Stellen vor dem Komma<br />
b) 10.1635 runden auf 3 Stellen -&gt; 10.164</p>
<pre><code>a)
float f = 10.1635;
int i = static_cast&lt;int&gt;(f);
</code></pre>
<p>Nun hat sich das Problem darauf reduziert festzustellen, wieviel Stellen eine Integerzahl hat. Sukzessives anwenden von div 10 ( / 10) sollte das gewueschte erziehlen.</p>
<pre><code>b)
float f = 10.1635;
float r = static_cast&lt;float&gt;( static_cast&lt;int&gt;(f * 10^3 + 0.5) );
</code></pre>
<p>Das rundet die gegebene float-Zahl auf 3 Stellen nach dem Komma. Es ist leicht auf n zu verallgemeinern. Auch solltest du bei negativen Zahlen aufpassen und entsprechend dein Programm anpassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660051</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660051</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Sun, 08 Feb 2009 16:33:05 GMT</pubDate></item><item><title><![CDATA[Reply to float Anzahl Stellen vor Komma on Sun, 08 Feb 2009 16:38:03 GMT]]></title><description><![CDATA[<p>Ich habe mal die Forensuche bemueht und ueberraschend viele Treffer erhalten. Solltest du auch machen! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660053</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660053</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Sun, 08 Feb 2009 16:38:03 GMT</pubDate></item><item><title><![CDATA[Reply to float Anzahl Stellen vor Komma on Sun, 08 Feb 2009 16:39:26 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<pre><code>a)
float f = 10.1635;
int i = static_cast&lt;int&gt;(f);
</code></pre>
<p>[...]</p>
<pre><code>b)
float f = 10.1635;
float r = static_cast&lt;float&gt;( static_cast&lt;int&gt;(f * 10^3 + 0.5) );
</code></pre>
</blockquote>
<p>Diese beiden Code-Stücke funktionieren nur selten korrekt. Beide gehen schief, sobald die Fließkomma-Zahl zu groß ist für einen int, was schnell vorkommt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660054</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660054</guid><dc:creator><![CDATA[Christoph]]></dc:creator><pubDate>Sun, 08 Feb 2009 16:39:26 GMT</pubDate></item><item><title><![CDATA[Reply to float Anzahl Stellen vor Komma on Fri, 06 Mar 2009 09:20:37 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<p>Ich habe mal die Forensuche bemueht und ueberraschend viele Treffer erhalten. Solltest du auch machen! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
</blockquote>
<p>Kurze Frage dazu:</p>
<p>Mit welchen Suchbegriffen denn? Versuche es nämlich auch gerade, leider nur mit mäßigem Erfolg!</p>
<p>Ciao</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1675076</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1675076</guid><dc:creator><![CDATA[Reth]]></dc:creator><pubDate>Fri, 06 Mar 2009 09:20:37 GMT</pubDate></item><item><title><![CDATA[Reply to float Anzahl Stellen vor Komma on Fri, 06 Mar 2009 09:31:50 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/21632">@knivil</a><br />
Dein Rundungscode ist falsch. Schau bitte nochmal nach was der operator ^ macht (jedenfalls keine Potenzierung).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1675079</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1675079</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Fri, 06 Mar 2009 09:31:50 GMT</pubDate></item><item><title><![CDATA[Reply to float Anzahl Stellen vor Komma on Fri, 06 Mar 2009 14:07:15 GMT]]></title><description><![CDATA[<p>Um zu runden, kann man die Funktionen <code>floor()</code> und <code>ceil()</code> aus <code>&lt;cmath&gt;</code> verwenden. Bei Bedarf passt man die Zahl durch Multiplikation/Division mit Zehnerpotenzen vorher und nachher an.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1675297</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1675297</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 06 Mar 2009 14:07:15 GMT</pubDate></item><item><title><![CDATA[Reply to float Anzahl Stellen vor Komma on Fri, 06 Mar 2009 15:31:44 GMT]]></title><description><![CDATA[<blockquote>
<p>Schau bitte nochmal nach was der operator ^ macht (jedenfalls keine Potenzierung)</p>
</blockquote>
<p>Ja genau und das b) im Code funktioniert so auch nicht etc. ... Ich bin net bloed.Eigentlich sollte es 10 hoch 3 oder allgemeiner 10 hoch x ausdruecken, aber du kannst es auch gern als copy-paste-Sperre interpretieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1675367</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1675367</guid><dc:creator><![CDATA[grad faul]]></dc:creator><pubDate>Fri, 06 Mar 2009 15:31:44 GMT</pubDate></item></channel></rss>