<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9 Forums - Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
	<atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Forums/rss"></atom:link>
	<image>
		<url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
		<title>Channel 9 Forums - Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<link>http://channel9.msdn.com/Forums</link>
	</image>
	<description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
	<link>http://channel9.msdn.com/Forums</link>
	<language>en</language>
	<pubDate>Tue, 21 May 2013 19:24:17 GMT</pubDate>
	<lastBuildDate>Tue, 21 May 2013 19:24:17 GMT</lastBuildDate>
	<generator>Rev9</generator>
	<c9:totalResults>11</c9:totalResults>
	<c9:pageCount>-11</c9:pageCount>
	<c9:pageSize>-1</c9:pageSize>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p>I've been doing C# development since the early days of .NET but for some reason I still can't figure&nbsp;the following&nbsp;out...</p><p>We have float.ToString(&quot;Fn&quot;), where &quot;n&quot; specifies the number of digits&nbsp;you want after the point. So that will convert &quot;123.456&quot; to &quot;123.46&quot;. The <a href="http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#FFormatString">documentation</a> calls the &quot;F&quot; formatting string the &quot;precision&quot; specifier and even says: &quot;<em>The precision specifier indicates the desired number of decimal places</em>&quot;.</p><p>Maybe I'm missing something but&nbsp;&quot;precision&quot; and &quot;decimal place&quot; aren't exactly the same thing. To me, &quot;precision&quot; means number of <strong>signinficant</strong> digits.</p><p>So according to&nbsp;me, the following values all have the same precision:</p><p><pre class="brush: text">0.0012345
0.012345
0.12345
1.2345
12.345
123.45
1234.5
12345
123450
1234500
</pre></p><p>...yet if you were to specify ToString(&quot;F5&quot;), the output won't look anything like the above.</p><p>So let's say I want to display a floating point value that can have a large range with a certain <em>precision</em>, how do I do that? Up to now I have used an algorithm that uses log10 to calculate the&nbsp;location of the&nbsp;point and then calculates the correct value for n in ToString(&quot;Fn&quot;).</p><p>Is there a better way?</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/e6d24d45df72454d9fd7a04f01343a37#e6d24d45df72454d9fd7a04f01343a37</link>
		<pubDate>Sat, 12 May 2012 18:42:13 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/e6d24d45df72454d9fd7a04f01343a37#e6d24d45df72454d9fd7a04f01343a37</guid>
		<dc:creator>BitFlipper</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/BitFlipper/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p>@<a href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision#ce6d24d45df72454d9fd7a04f01343a37">BitFlipper</a>: &quot;Gn&quot; where n is &quot;<span>Precision specifier: Number of significant digits.&quot;</span></p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/9e0b472b49cb43b3bb53a04f0143c86f#9e0b472b49cb43b3bb53a04f0143c86f</link>
		<pubDate>Sat, 12 May 2012 19:38:51 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/9e0b472b49cb43b3bb53a04f0143c86f#9e0b472b49cb43b3bb53a04f0143c86f</guid>
		<dc:creator>DCMonkey</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/DCMonkey/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/9e0b472b49cb43b3bb53a04f0143c86f">26 minutes&nbsp;ago</a>, <a href="/Niners/DCMonkey">DCMonkey</a> wrote</p><p>@<a href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision#ce6d24d45df72454d9fd7a04f01343a37">BitFlipper</a>: &quot;Gn&quot; where n is &quot;<span>Precision specifier: Number of significant digits.&quot;</span></p><p></p></div></blockquote><p></p><p>The problem with &quot;Gn&quot; is that it automatically switches between fixed-point and scientific notation, whichever is more compact. In my case I always want it to be in fixed-point notation.</p><p>It&nbsp;doesn't look like&nbsp;you can tell it to always use fixed point.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/18d24fac70a344fe8655a04f014c177a#18d24fac70a344fe8655a04f014c177a</link>
		<pubDate>Sat, 12 May 2012 20:09:06 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/18d24fac70a344fe8655a04f014c177a#18d24fac70a344fe8655a04f014c177a</guid>
		<dc:creator>BitFlipper</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/BitFlipper/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/18d24fac70a344fe8655a04f014c177a">15 hours&nbsp;ago</a>, <a href="/Niners/BitFlipper">BitFlipper</a> wrote</p><p>*snip*</p><p>The problem with &quot;Gn&quot; is that it automatically switches between fixed-point and scientific notation, whichever is more compact. In my case I always want it to be in fixed-point notation.</p><p>It&nbsp;doesn't look like&nbsp;you can tell it to always use fixed point.</p><p></p></div></blockquote><p></p><p>&nbsp;</p><p>well &quot;floating point&quot;&nbsp; by definition is not fixed ....</p><p>what about &quot;R&quot;&nbsp; format as that is to make sure you can get back the same value ?</p><p>seems like that is really what you want ?</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/e185b7e8c60f4418a53da05000c8164e#e185b7e8c60f4418a53da05000c8164e</link>
		<pubDate>Sun, 13 May 2012 12:08:29 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/e185b7e8c60f4418a53da05000c8164e#e185b7e8c60f4418a53da05000c8164e</guid>
		<dc:creator>figuerres</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/figuerres/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p>@<a href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision#c18d24fac70a344fe8655a04f014c177a">BitFlipper</a>: I'm afraid I don't understand your specification. How would you represent 1E8 in fixed point notation when you specify seven significant digits?</p><p>&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/d3b20f612560400ea314a05001083299#d3b20f612560400ea314a05001083299</link>
		<pubDate>Sun, 13 May 2012 16:01:54 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/d3b20f612560400ea314a05001083299#d3b20f612560400ea314a05001083299</guid>
		<dc:creator>Blue Ink</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Blue Ink/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/e185b7e8c60f4418a53da05000c8164e">4 hours ago</a>, <a href="/Niners/figuerres">figuerres</a> wrote</p><p>*snip*&nbsp;</p><p>well &quot;floating point&quot;&nbsp; by definition is not fixed ....</p><p>what about &quot;R&quot;&nbsp; format as that is to make sure you can get back the same value ?</p><p>seems like that is really what you want ?</p></div></blockquote><p></p><p>&quot;R&quot; won't work either because it always uses the max precision that the float or double&nbsp;supports. I specifically want it to be rounded to a specified precision.&nbsp;I need this for display purposes, not round-trip purposes.</p><p>As far as calling it &quot;fixed point&quot;, I'm simply using the same terminology that the documentation is using. Is this another example where the documentation is using the wrong terminology? It specifically states &quot;<em>The general (&quot;G&quot;) format specifier converts a number to the most compact of either fixed-point or scientific notation</em>&quot;. The &quot;fixed-point&quot; is referring to something like &quot;123.456&quot;. I'm not a math expert but I always thought &quot;fixed point&quot; implied that the point is fixed at a certain location, not that it is necessarily an integer.</p><p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/d3b20f612560400ea314a05001083299">12 minutes ago</a>, <a href="/Niners/Blue%20Ink">Blue Ink</a> wrote</p><p>@<a href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision#c18d24fac70a344fe8655a04f014c177a">BitFlipper</a>: I'm afraid I don't understand your specification. How would you represent 1E8 in fixed point notation when you specify seven significant digits?&nbsp;</p><p></p></div></blockquote><p></p><p>It would be represented as &quot;0.00000001&quot;. The&nbsp;specified precision&nbsp;is the max number of <strong>significant</strong> digits that should be returned. If the specific number has more digits than what is specified by the precision argument, it should be rounded to only have that number of significant digits.</p><p>Also, looking at the documentation for float and double, it specifies the &quot;precision&quot; that these types have, which is 7 for float and 15 for double. However each of these have a very large range. For instance float goes from 1E-44 to 3.4E&#43;38, however it always has at least 7 digits of precision. This means that it can represent 0.000001234567 as well as 1234567000000 with the same 7 digits of precision.</p><p>Here are some examples:</p><p>Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Precision&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result<br>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<br>10000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10000<br>12345&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12345&nbsp;&nbsp;&nbsp;&nbsp;&lt;- 12300 doesn't make sense in this case<br>12.345&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12.35<br>0.0000000123&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.000000012<br>0.99999&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1</p><p>Negative numbers have the exact same result other having &quot;-&quot; prepended as expected.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/ad844c03aa12474db2ffa05001124546#ad844c03aa12474db2ffa05001124546</link>
		<pubDate>Sun, 13 May 2012 16:38:35 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/ad844c03aa12474db2ffa05001124546#ad844c03aa12474db2ffa05001124546</guid>
		<dc:creator>BitFlipper</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/BitFlipper/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/ad844c03aa12474db2ffa05001124546">11 minutes&nbsp;ago</a>, <a href="/Niners/BitFlipper">BitFlipper</a> wrote</p><p>*snip*</p><p>This means that it can represent 0.000001234567 as well as 1234567000000 with the same 7 digits of precision.</p><p></p></div></blockquote><p></p><p>No. That's a matter of accuracy. Accuracy and precision are very different beasts.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/a261e291205848caa82fa0500115b81b#a261e291205848caa82fa0500115b81b</link>
		<pubDate>Sun, 13 May 2012 16:51:08 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/a261e291205848caa82fa0500115b81b#a261e291205848caa82fa0500115b81b</guid>
		<dc:creator>AndyC</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/AndyC/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText"><p></p><p><a class="permalink" title="Post Permalink" href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/a261e291205848caa82fa0500115b81b">7 minutes&nbsp;ago</a>, <a href="/Niners/AndyC">AndyC</a> wrote</p><p>*snip*</p><p>No. That's a matter of accuracy. Accuracy and precision are very different beasts.</p><p></p></div></blockquote><p></p><p>I'm well aware of the difference between accuracy and precision, but I think they are the other way around from what you just said. <a href="http://en.wikipedia.org/wiki/Arithmetic_precision">Wikipedia states</a> &quot;<em>The <strong>precision</strong> of a value describes the <strong>number of digits</strong> that are used to express that value</em>&quot;.</p><p>The <a href="http://msdn.microsoft.com/en-us/library/9ahet949.aspx">MSDN documentation states</a> that float has a <strong>precision</strong> of <strong>7 digits</strong> and double has a <strong>precision</strong> of <strong>15 digits</strong>.</p><p>So in both cases precision == number of digits, which is exactly what I mean when saying &quot;precision&quot;. None of what I have said so far addresses &quot;accuracy&quot; which has more to do with the source of the values than how to represent them.</p><p>Actually, one of the reasons I need to specify the precision is exactly because this data isn't very accurate, so using 7 digits to represent it is meaningless. I want to specify 3 or 4 digits.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/e50c5c4ef3684dc58b90a050011b8a7a#e50c5c4ef3684dc58b90a050011b8a7a</link>
		<pubDate>Sun, 13 May 2012 17:12:20 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/e50c5c4ef3684dc58b90a050011b8a7a#e50c5c4ef3684dc58b90a050011b8a7a</guid>
		<dc:creator>BitFlipper</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/BitFlipper/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p>@<a href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision#ce50c5c4ef3684dc58b90a050011b8a7a">BitFlipper</a>: But the only thing being guaranteed is the precision (i.e the minimum number of significant digits you will get back). How accurately they will represent the number you were intending to store is a whole different kettle of fish.</p><p>There is no guarantee that two numbers which <em>require</em> the same level of precision (such as 0.000001234567&nbsp;and 1234567000000 ) will actually both be stored to the same level of accuracy (in fact, it's pretty much guaranteed they won't for floating point math, as there are numbers with far less significant digits that can't be accurately represented.)</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/18fff66a37b24cb1be96a0510130fce6#18fff66a37b24cb1be96a0510130fce6</link>
		<pubDate>Mon, 14 May 2012 18:30:25 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/18fff66a37b24cb1be96a0510130fce6#18fff66a37b24cb1be96a0510130fce6</guid>
		<dc:creator>AndyC</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/AndyC/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p>@<a href="/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision#c18fff66a37b24cb1be96a0510130fce6">AndyC</a>:</p><p>Yes that is correct however the issue here is simply <em>displaying</em> the number with a consistent number of significant digits, no matter the accuracy. All I want is a way to display a series of numbers that each could have values that are multiple orders of magnitude different from each other. So I want to display 12345 or 0.0012345. The currently available formatting options don't allow you to do that.</p><p>BTW, 0.000001234567 and 1234567000000&nbsp;have no problem&nbsp;maintaining&nbsp;the stated&nbsp;precision (at least 7 digits for&nbsp;float or&nbsp;at least 15 for double), no matter that their values are so hugely different. Yes there are numbers they can't represent (like 1/10), but the error is still below the specified precision. Where you do lose precision is when you add/subtract values with large differences in size (this is a real problem in&nbsp;some types of digital&nbsp;filters for instance because the coefficients could have extreme large/small value).</p><p>Although none of the accuracy related issues are really very relevant to the problem I'm trying to solve.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/d22bdd97f7ed47938640a0510153d176#d22bdd97f7ed47938640a0510153d176</link>
		<pubDate>Mon, 14 May 2012 20:37:14 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/d22bdd97f7ed47938640a0510153d176#d22bdd97f7ed47938640a0510153d176</guid>
		<dc:creator>BitFlipper</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/BitFlipper/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Coffeehouse - Silly question: How to format a float value with a specified precision?</title>
		<description><![CDATA[<p>I've experimented with many different ways to get the results I want. Using approaches like Log10 suffers from rounding issues (because Math.Log10(99999.9999999999) equates to 5.0). So the simple algorithm below seems to give correct results in all tested cases up to now, including the edge cases that trips up the other algorithms. Not sure this is the cleanest or most accurate way, but it works for my purposes.&nbsp;</p><p>Maybe the code below will clarify what I need to those that don't seem to understand the results I'm after.</p><p><pre class="brush: csharp">private static string ToString(this double value, int precision, bool showPlus = false)
{
    var text = &quot;&quot;;

    if (value == 0.0)
    {
        text = &quot;0&quot;;

        if (precision &gt; 1)
            text &#43;= &quot;.&quot;.PadRight(precision, '0');
    }
    else
    {
        var absValue = Math.Abs(value);
        var threshold = (long)Math.Pow(10.0, precision - 1);
        var shift = 0;
        var shiftValue = absValue;
        var longValue = (long)Math.Round(shiftValue);

        while (longValue &lt; threshold)
        {
            shiftValue *= 10.0;
            longValue = (long)Math.Round(shiftValue);
            shift&#43;&#43;;
        }

        text = absValue.ToString(&quot;F&quot; &#43; shift);
    }

    if (value &lt; 0.0)
    {
        return &quot;-&quot; &#43; text;
    }
    else if (value &gt; 0.0 &amp;&amp; showPlus)
    {
        return &quot;&#43;&quot; &#43; text;
    }
    else
    {
        return text;
    }
}
</pre></p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/1fdd64d49fb84cf69f3ea05201054b10#1fdd64d49fb84cf69f3ea05201054b10</link>
		<pubDate>Tue, 15 May 2012 15:51:20 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Silly-question-How-to-format-a-float-value-with-a-specified-precision/1fdd64d49fb84cf69f3ea05201054b10#1fdd64d49fb84cf69f3ea05201054b10</guid>
		<dc:creator>BitFlipper</dc:creator>
		<slash:comments>11</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/BitFlipper/Discussions/RSS</wfw:commentRss>
	</item>
</channel>
</rss>