<?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>Gustavo Henrique.net &#187; formatar moeda</title>
	<atom:link href="http://www.gustavohenrique.net/brogui/tag/formatar-moeda/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gustavohenrique.net/brogui</link>
	<description>Só mais um blog com Wordpress</description>
	<lastBuildDate>Tue, 29 Jun 2010 00:00:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Formatando decimal para moeda brasileira</title>
		<link>http://www.gustavohenrique.net/brogui/2009/04/formatando-decimal-para-moeda-brasileira/</link>
		<comments>http://www.gustavohenrique.net/brogui/2009/04/formatando-decimal-para-moeda-brasileira/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 02:59:41 +0000</pubDate>
		<dc:creator>gustavohenrique</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[formatar moeda]]></category>
		<category><![CDATA[moeda brasileira]]></category>
		<category><![CDATA[real]]></category>

		<guid isPermaLink="false">http://www.gustavohenrique.net/brogui/?p=96</guid>
		<description><![CDATA[O código abaixo mostra uma maneira simples de formatar números decimais (float) para a moeda brasileira: Real. É um exemplo simples que o método retorna uma string no formato R$ X.XXX,XX.

def moeda_brasileira&#40;numero&#41;:
    &#34;&#34;&#34;
    Retorna uma string no formato de moeda brasileira
    &#34;&#34;&#34;
&#160;
    try:
 [...]]]></description>
			<content:encoded><![CDATA[<p>O código abaixo mostra uma maneira simples de formatar números decimais (float) para a moeda brasileira: Real. É um exemplo simples que o método retorna uma string no formato R$ X.XXX,XX.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> moeda_brasileira<span style="color: black;">&#40;</span>numero<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Retorna uma string no formato de moeda brasileira
    &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        contador = <span style="color: #ff4500;">0</span>
        preco_str = <span style="color: #483d8b;">''</span>
        num = numero.<span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'.'</span> <span style="color: #ff7700;font-weight:bold;">in</span> num:
            preco, centavos = num.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            preco = num
            centavos = <span style="color: #483d8b;">'00'</span>
&nbsp;
        tamanho = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>preco<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> tamanho <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
            preco_str = preco_str + preco<span style="color: black;">&#91;</span>tamanho-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
            contador += <span style="color: #ff4500;">1</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> contador == <span style="color: #ff4500;">3</span> <span style="color: #ff7700;font-weight:bold;">and</span> tamanho <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span>:
                    preco_str = preco_str + <span style="color: #483d8b;">'.'</span>
                    contador = <span style="color: #ff4500;">0</span>
            tamanho -= <span style="color: #ff4500;">1</span>
&nbsp;
        tamanho = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>preco_str<span style="color: black;">&#41;</span>
        str_preco = <span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> tamanho <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
            str_preco = str_preco + preco_str<span style="color: black;">&#91;</span>tamanho-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
            tamanho -= <span style="color: #ff4500;">1</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;R$ %s,%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>str_preco, centavos<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'Erro. Nao foi possivel formatar.'</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Produto<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    modelo = CharField<span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>
    preco = DecimalField<span style="color: black;">&#40;</span>max_digits=<span style="color: #ff4500;">10</span>,decimal_places=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__unicode__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">modelo</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> getPrecoFormatado<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        p = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">preco</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> p <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'None'</span>:
            preco = moeda_brasileira<span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> preco
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
    getPrecoFormatado.<span style="color: black;">short_description</span> = <span style="color: #483d8b;">'Preço'</span></pre></div></div>

<p>Exemplo de uso:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span>produto = Produto.<span style="color: black;">objects</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span>=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span>produto.<span style="color: black;">getPrecoFormatado</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'R$ 1.290,00'</span></pre></div></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Compartilhe esse artigo</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F&amp;submitHeadline=Formatando+decimal+para+moeda+brasileira&amp;submitSummary=" rel="nofollow" title="Adicionar ao&nbsp;Buzz"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/buzz.png" title="Adicionar ao&nbsp;Buzz" alt="Adicionar ao&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F&amp;title=Formatando+decimal+para+moeda+brasileira" rel="nofollow" title="Adicionar ao&nbsp;Del.icio.us"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/delicious.png" title="Adicionar ao&nbsp;Del.icio.us" alt="Adicionar ao&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F&amp;title=Formatando+decimal+para+moeda+brasileira" rel="nofollow" title="Adicionar ao&nbsp;digg"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/digg.png" title="Adicionar ao&nbsp;digg" alt="Adicionar ao&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F" rel="nofollow" title="Adicionar ao&nbsp;Facebook"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/facebook.png" title="Adicionar ao&nbsp;Facebook" alt="Adicionar ao&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F&amp;title=Formatando+decimal+para+moeda+brasileira" rel="nofollow" title="Adicionar ao&nbsp;Google Bookmarks"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/google.png" title="Adicionar ao&nbsp;Google Bookmarks" alt="Adicionar ao&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F&amp;bm_description=Formatando+decimal+para+moeda+brasileira" rel="nofollow" title="Adicionar ao&nbsp;Mister Wong"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Adicionar ao&nbsp;Mister Wong" alt="Adicionar ao&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F&amp;T=Formatando+decimal+para+moeda+brasileira" rel="nofollow" title="Adicionar ao&nbsp;Netscape"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/netscape.png" title="Adicionar ao&nbsp;Netscape" alt="Adicionar ao&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F&amp;title=Formatando+decimal+para+moeda+brasileira" rel="nofollow" title="Adicionar ao&nbsp;reddit"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/reddit.png" title="Adicionar ao&nbsp;reddit" alt="Adicionar ao&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F&amp;title=Formatando+decimal+para+moeda+brasileira" rel="nofollow" title="Adicionar ao&nbsp;Stumble Upon"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Adicionar ao&nbsp;Stumble Upon" alt="Adicionar ao&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F" rel="nofollow" title="Adicionar ao&nbsp;Technorati"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/technorati.png" title="Adicionar ao&nbsp;Technorati" alt="Adicionar ao&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F" rel="nofollow" title="Adicionar ao&nbsp;Tip'd"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/tipd.png" title="Adicionar ao&nbsp;Tip'd" alt="Adicionar ao&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Formatando+decimal+para+moeda+brasileira+@+http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F" rel="nofollow" title="Adicionar ao&nbsp;Twitter"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/twitter.png" title="Adicionar ao&nbsp;Twitter" alt="Adicionar ao&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F04%2Fformatando-decimal-para-moeda-brasileira%2F&amp;t=Formatando+decimal+para+moeda+brasileira" rel="nofollow" title="Adicionar ao&nbsp;Yahoo My Web"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Adicionar ao&nbsp;Yahoo My Web" alt="Adicionar ao&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.gustavohenrique.net/brogui/2009/04/formatando-decimal-para-moeda-brasileira/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
