<?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 to string nachbauen]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe einen kleinen task gecoded:<br />
ich weiss das 7.64 nicht als floating point representiert werden kann...aber warum bekomme ich 7.995886933 als ergebnis?</p>
<p>/*Question:<br />
Given a float number 7.64, convert it into the string WITHOUT using any inbuilt function/library.<br />
for eg:<br />
input<br />
float no.: 7.64<br />
output<br />
string: 7.64<br />
*/</p>
<p>template &lt;class T&gt;<br />
string to_string(T &amp;t)<br />
{<br />
stringstream ss;<br />
ss &lt;&lt; t;<br />
return ss.str();<br />
}</p>
<p>string float_to_string(float val) {<br />
int integral = static_cast&lt;int&gt;(val);<br />
int fractional = 0;</p>
<p>val = val - integral;<br />
while(val &gt; 0) {<br />
val = val * 10;<br />
fractional = (fractional * 10) + static_cast&lt;int&gt;(val);<br />
val = val - static_cast&lt;int&gt;(val);<br />
}</p>
<p>string str = to_string&lt;int&gt;(integral) + '.' + to_string&lt;int&gt;(fractional);</p>
<p>return str;<br />
}</p>
<p>int main() {<br />
cout &lt;&lt; float_to_string(7.64) &lt;&lt; endl;<br />
}</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/326437/float-to-string-nachbauen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 00:54:55 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/326437.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 18 Jun 2014 09:41:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to float to string nachbauen on Wed, 18 Jun 2014 09:41:21 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe einen kleinen task gecoded:<br />
ich weiss das 7.64 nicht als floating point representiert werden kann...aber warum bekomme ich 7.995886933 als ergebnis?</p>
<p>/*Question:<br />
Given a float number 7.64, convert it into the string WITHOUT using any inbuilt function/library.<br />
for eg:<br />
input<br />
float no.: 7.64<br />
output<br />
string: 7.64<br />
*/</p>
<p>template &lt;class T&gt;<br />
string to_string(T &amp;t)<br />
{<br />
stringstream ss;<br />
ss &lt;&lt; t;<br />
return ss.str();<br />
}</p>
<p>string float_to_string(float val) {<br />
int integral = static_cast&lt;int&gt;(val);<br />
int fractional = 0;</p>
<p>val = val - integral;<br />
while(val &gt; 0) {<br />
val = val * 10;<br />
fractional = (fractional * 10) + static_cast&lt;int&gt;(val);<br />
val = val - static_cast&lt;int&gt;(val);<br />
}</p>
<p>string str = to_string&lt;int&gt;(integral) + '.' + to_string&lt;int&gt;(fractional);</p>
<p>return str;<br />
}</p>
<p>int main() {<br />
cout &lt;&lt; float_to_string(7.64) &lt;&lt; endl;<br />
}</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404367</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404367</guid><dc:creator><![CDATA[wernerschwarz]]></dc:creator><pubDate>Wed, 18 Jun 2014 09:41:21 GMT</pubDate></item><item><title><![CDATA[Reply to float to string nachbauen on Wed, 18 Jun 2014 09:43:58 GMT]]></title><description><![CDATA[<p>Dein Code formatiert und in Tags...</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

using namespace std;

template &lt;class T&gt;
string to_string(T &amp;t)
{
	stringstream ss;
	ss &lt;&lt; t;
	return ss.str();
}

string float_to_string(float val)
{
	int integral = static_cast&lt;int&gt;(val);
	int fractional = 0;

	val -= integral;
	while(val &gt; 0)
	{
		val *= 10;
		fractional = (fractional * 10) + static_cast&lt;int&gt;(val);
		val -= static_cast&lt;int&gt;(val);
	}

	string str = to_string&lt;int&gt;(integral) + '.' + to_string&lt;int&gt;(fractional);

	return str;
}

int main()
{
	cout &lt;&lt; float_to_string(7.64) &lt;&lt; endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2404369</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404369</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 18 Jun 2014 09:43:58 GMT</pubDate></item><item><title><![CDATA[Reply to float to string nachbauen on Wed, 18 Jun 2014 09:48:02 GMT]]></title><description><![CDATA[<blockquote>
<p>aber warum bekomme ich 7.995886933 als ergebnis?</p>
</blockquote>
<p>Weil Fließkommazahlen anders funktionieren als du denkst. Die Standardausgabe gibt Fließkommazahlen immer nur bis zu einer bestimmten vorgegebenen Präzision aus - es ist nämlich völlig Unsinnig, alle Ziffern der gespeicherten Fließkommazahl auszugeben.</p>
<p>Versuche also, deine <code>to_string</code> Funktion nur zwei Nachkommastellen ausgeben zu lassen. Oder die Schleife nur maximal fünf Durchlaufe zu lassen. Da bekomme ich schon <code>7.6399</code> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404371</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404371</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 18 Jun 2014 09:48:02 GMT</pubDate></item><item><title><![CDATA[Reply to float to string nachbauen on Wed, 18 Jun 2014 09:50:52 GMT]]></title><description><![CDATA[<p>soll der user die anzahl der nachkomma-stellen angeben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404373</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404373</guid><dc:creator><![CDATA[wernerschwarz]]></dc:creator><pubDate>Wed, 18 Jun 2014 09:50:52 GMT</pubDate></item><item><title><![CDATA[Reply to float to string nachbauen on Wed, 18 Jun 2014 09:54:17 GMT]]></title><description><![CDATA[<p>also:</p>
<pre><code>#include &lt;iostream&gt; 
#include &lt;string&gt; 
#include &lt;sstream&gt; 

using namespace std; 

template &lt;class T&gt; 
string to_string(T &amp;t) 
{ 
    stringstream ss; 
    ss &lt;&lt; t; 
    return ss.str(); 
} 

string float_to_string(float val, int n) 
{ 
    int integral = static_cast&lt;int&gt;(val); 
    int fractional = 0; 

    val -= integral; 
    while(val &gt; 0 &amp;&amp; n &gt; 0) 
    { 
        val *= 10; 
        fractional = (fractional * 10) + static_cast&lt;int&gt;(val); 
        val -= static_cast&lt;int&gt;(val); 
        n--;
    } 

    string str = to_string&lt;int&gt;(integral) + '.' + to_string&lt;int&gt;(fractional); 

    return str; 
} 

int main() 
{ 
    cout &lt;&lt; float_to_string(7.64, 4) &lt;&lt; endl; 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2404376</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404376</guid><dc:creator><![CDATA[wernerschwarz]]></dc:creator><pubDate>Wed, 18 Jun 2014 09:54:17 GMT</pubDate></item></channel></rss>