<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alex Medina &#187; Format</title>
	<atom:link href="http://www.alexmedina.net/blog/tag/format/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alexmedina.net/blog</link>
	<description>Porque todos los días se aprende algo nuevo...</description>
	<lastBuildDate>Thu, 22 Dec 2011 09:40:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>String Format for Double [C#]</title>
		<link>http://www.alexmedina.net/blog/2009/06/29/string-format-for-double-c/</link>
		<comments>http://www.alexmedina.net/blog/2009/06/29/string-format-for-double-c/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 15:39:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Format]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://alexmedina.net/blog/2009/06/29/string-format-for-double-c/</guid>
		<description><![CDATA[The following examples show how to format float numbers to string in C#. You can use static method String.Format or instance methods double.ToString and float.ToString. Digits after decimal point This example formats double to string with fixed number of decimal places. For two decimal places use pattern „0.00“. If a float number has less decimal [...]]]></description>
			<content:encoded><![CDATA[<p>The following examples show how to format float numbers to string in C#. You can use static method <strong><a href="http://msdn2.microsoft.com/en-us/library/system.string.format.aspx" rel="nofollow">String.Format</a></strong> or instance methods <a href="http://msdn2.microsoft.com/en-us/library/kfsatb94.aspx">double.ToString</a> and <a href="http://msdn2.microsoft.com/en-us/library/f71z6k0c.aspx">float.ToString</a>.</p>
<h2>Digits after decimal point</h2>
<p>This example formats double to string with <strong>fixed number of decimal places</strong>. For two decimal places use pattern „<strong>0.00</strong>“. If a float number has less decimal places, the rest digits on the right will be zeroes. If it has more decimal places, the number will be rounded.</p>
<p>[C#]</p>
<pre class="code"><span class="comments">// just two decimal places</span>
<span class="type">String</span>.Format(<span class="string">"{0:0.00}"</span>, 123.4567);      <span class="comments">// "123.46"</span>
<span class="type">String</span>.Format(<span class="string">"{0:0.00}"</span>, 123.4);         <span class="comments">// "123.40"</span>
<span class="type">String</span>.Format(<span class="string">"{0:0.00}"</span>, 123.0);         <span class="comments">// "123.00"</span></pre>
<p>Next example formats double to string with <strong>floating number of decimal places</strong>. E.g. for maximal two decimal places use pattern „<strong>0.##</strong>“.</p>
<p>[C#]</p>
<pre class="code"><span class="comments">// max. two decimal places</span>
<span class="type">String</span>.Format(<span class="string">"{0:0.##}"</span>, 123.4567);      <span class="comments">// "123.46"</span>
<span class="type">String</span>.Format(<span class="string">"{0:0.##}"</span>, 123.4);         <span class="comments">// "123.4"</span>
<span class="type">String</span>.Format(<span class="string">"{0:0.##}"</span>, 123.0);         <span class="comments">// "123"</span></pre>
<h2>Digits before decimal point</h2>
<p>If you want a float number to have any <strong>minimal number of digits before decimal point</strong> use N-times zero before decimal point. E.g. pattern „<strong>00.0</strong>“ formats a float number to string with at least two digits before decimal point and one digit after that.</p>
<p>[C#]</p>
<pre class="code"><span class="comments">// at least two digits before decimal point</span>
<span class="type">String</span>.Format(<span class="string">"{0:00.0}"</span>, 123.4567);      <span class="comments">// "123.5"</span>
<span class="type">String</span>.Format(<span class="string">"{0:00.0}"</span>, 23.4567);       <span class="comments">// "23.5"</span>
<span class="type">String</span>.Format(<span class="string">"{0:00.0}"</span>, 3.4567);        <span class="comments">// "03.5"</span>
<span class="type">String</span>.Format(<span class="string">"{0:00.0}"</span>, -3.4567);       <span class="comments">// "-03.5"</span></pre>
<h2>Thousands separator</h2>
<p>To format double to string <strong>with use of thousands separator</strong> use zero and comma separator before an usual float formatting pattern, e.g. pattern „<strong>0,0.0</strong>“ formats the number to use thousands separators and to have one decimal place.</p>
<p>[C#]</p>
<pre class="code"><span class="type">String</span>.Format(<span class="string">"{0:0,0.0}"</span>, 12345.67);     <span class="comments">// "12,345.7"</span>
<span class="type">String</span>.Format(<span class="string">"{0:0,0}"</span>, 12345.67);       <span class="comments">// "12,346"</span></pre>
<h2>Zero</h2>
<p>Float <strong>numbers between zero and one</strong> can be formatted in two ways, with or without leading zero before decimal point. To format number without a leading zero use <strong>#</strong> before point. For example „<strong>#.0</strong>“ formats number to have one decimal place and zero to N digits before decimal point (e.g. „.5“ or „123.5“).</p>
<p>Following code shows <strong>how can be formatted a zero</strong> (of double type).</p>
<p>[C#]</p>
<pre class="code"><span class="type">String</span>.Format(<span class="string">"{0:0.0}"</span>, 0.0);            <span class="comments">// "0.0"</span>
<span class="type">String</span>.Format(<span class="string">"{0:0.#}"</span>, 0.0);            <span class="comments">// "0"</span>
<span class="type">String</span>.Format(<span class="string">"{0:#.0}"</span>, 0.0);            <span class="comments">// ".0"</span>
<span class="type">String</span>.Format(<span class="string">"{0:#.#}"</span>, 0.0);            <span class="comments">// ""</span></pre>
<h2>Align numbers with spaces</h2>
<p>To align float number <strong>to the right</strong> use comma „<strong>,</strong>“ option before the colon. Type comma followed by a number of spaces, e.g. „<strong>0,10:0.0</strong>“ (this can be used only in <a href="http://msdn2.microsoft.com/en-us/library/system.string.format.aspx" rel="nofollow">String.Format</a> method, not in <a href="http://msdn2.microsoft.com/en-us/library/kfsatb94.aspx">double.ToString</a> method). To align numbers <strong>to the left</strong> use negative number of spaces.</p>
<p>[C#]</p>
<pre class="code"><span class="type">String</span>.Format(<span class="string">"{0,10:0.0}"</span>, 123.4567);    <span class="comments">// "     123.5"</span>
<span class="type">String</span>.Format(<span class="string">"{0,-10:0.0}"</span>, 123.4567);   <span class="comments">// "123.5     "</span>
<span class="type">String</span>.Format(<span class="string">"{0,10:0.0}"</span>, -123.4567);   <span class="comments">// "    -123.5"</span>
<span class="type">String</span>.Format(<span class="string">"{0,-10:0.0}"</span>, -123.4567);  <span class="comments">// "-123.5    "</span></pre>
<h2>Custom formatting for negative numbers and zero</h2>
<p>If you need to use custom format for negative float numbers or zero, use <strong>semicolon separator</strong> „<strong>;</strong>“ to split pattern to <strong>three sections</strong>. The first section formats positive numbers, the <strong>second section formats negative numbers</strong> and the third section formats zero. If you omit the last section, zero will be formatted using the first section.</p>
<p>[C#]</p>
<pre class="code"><span class="type">String</span>.Format(<span class="string">"{0:0.00;minus 0.00;zero}"</span>, 123.4567);   <span class="comments">// "123.46"</span>
<span class="type">String</span>.Format(<span class="string">"{0:0.00;minus 0.00;zero}"</span>, -123.4567);  <span class="comments">// "minus 123.46"</span>
<span class="type">String</span>.Format(<span class="string">"{0:0.00;minus 0.00;zero}"</span>, 0.0);        <span class="comments">// "zero"</span></pre>
<h2>Some funny examples</h2>
<p>As you could notice in the previous example, you can put any text into formatting pattern, e.g. before an usual pattern „<strong><em>my text </em>0.0</strong>“. You can even put any text between the zeroes, e.g. „<strong>0<em>aaa</em>.<em>bbb</em>0</strong>“.</p>
<p>[C#]</p>
<pre class="code"><span class="type">String</span>.Format(<span class="string">"{0:my number is 0.0}"</span>, 12.3);   <span class="comments">// "my number is 12.3"</span>
<span class="type">String</span>.Format(<span class="string">"{0:0aaa.bbb0}"</span>, 12.3);          <span class="comments">// "12aaa.bbb3"</span></pre>
<h2>See also</h2>
<ul>
<li><a href="http://www.csharp-examples.net/string-format-int/">[C#] String Format for Int</a> – format 	(align) integer numbers</li>
<li><a href="http://www.csharp-examples.net/string-format-datetime/">[C#] String Format for DateTime</a> – 	format date and time</li>
<li><a href="http://www.csharp-examples.net/iformatprovider-numbers/">[C#] IFormatProvider for 	Numbers</a> – format and parse float numbers using CultureInfo</li>
<li><a href="http://www.csharp-examples.net/custom-iformatprovider/">[C#] Custom IFormatProvider</a> – 	string formatting with custom IFormatProvider</li>
<li><a href="http://www.csharp-examples.net/align-string-with-spaces/">[C#] Align String with Spaces</a> – 	how to align text to the right or left</li>
<li><a href="http://www.csharp-examples.net/indent-string-with-spaces/">[C#] Indent String with 	Spaces</a> – how to indent text with repeated spaces</li>
</ul>
<ul>
<li><a href="http://msdn2.microsoft.com/en-us/library/7x5bacwt.aspx" rel="nofollow">MSDN Examples</a> – MSDN examples for custom numeric 	formatting</li>
<li><a href="http://msdn2.microsoft.com/en-us/library/system.string.format.aspx" rel="nofollow">String.Format</a> – MSDN – method to format strings</li>
<li><a href="http://msdn2.microsoft.com/en-us/library/kfsatb94.aspx" rel="nofollow">Double.ToString</a> – MSDN – formats double to string</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.alexmedina.net/blog/2009/06/29/string-format-for-double-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

