<?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>Tortoise IT &#187; Convert Currency</title>
	<atom:link href="http://www.sean-barton.co.uk/tag/convert-currency/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sean-barton.co.uk</link>
	<description>by Sean Barton, a freelance PHP website developer in Crewe, Cheshire</description>
	<lastBuildDate>Tue, 31 Jan 2012 17:34:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Converting any number to a Currency (or 2 decimal places)</title>
		<link>http://www.sean-barton.co.uk/2009/05/converting-any-number-to-a-currency-or-2-decimal-places/</link>
		<comments>http://www.sean-barton.co.uk/2009/05/converting-any-number-to-a-currency-or-2-decimal-places/#comments</comments>
		<pubDate>Fri, 29 May 2009 15:31:56 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Time Savers]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Convert Currency]]></category>
		<category><![CDATA[Converting Currency]]></category>

		<guid isPermaLink="false">http://www.sean-barton.co.uk/?p=287</guid>
		<description><![CDATA[Yes it sounds fairly elementary doesn&#8217;t it&#8230; yet I can&#8217;t find a PHP function to just do it? If, like me, you store your currency values in your database as integers (don&#8217;t ask why, I just do) then you need to multiply by 100 on the way in and divide by 100 on the way out. The obvious problem (or maybe not so obvious) is that when multiplying a number 100.1 is different to 100.10 and therefore the former needs to be converted to 100.10 for the resultant number to be correct. The following function simply does that whilst checking &#8230; <a class="continue_reading" href="http://www.sean-barton.co.uk/2009/05/converting-any-number-to-a-currency-or-2-decimal-places/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Yes it sounds fairly elementary doesn&#8217;t it&#8230; yet I can&#8217;t find a PHP function to just do it?</p>
<p>If, like me, you store your currency values in your database as integers (don&#8217;t ask why, I just do) then you need to multiply by 100 on the way in and divide by 100 on the way out.</p>
<p>The obvious problem (or maybe not so obvious) is that when multiplying a number 100.1 is different to 100.10 and therefore the former needs to be converted to 100.10 for the resultant number to be correct.</p>
<p>The following function simply does that whilst checking for the occurence of a decimal place and concatenating .00 in that situation.</p>
<pre>function convert_to_currency($num) {
    if (strpos($num, '.') == false) {
        $num = $num . '.00';
    } else {
        $num = sprintf("%01.2f", (float)$num);
    }
    return $num;
}</pre>
<p>This should reliably format the number ready to be manipulated for either a database or to be directly output.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sean-barton.co.uk/2009/05/converting-any-number-to-a-currency-or-2-decimal-places/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 7/23 queries in 0.084 seconds using disk
Object Caching 323/336 objects using disk

Served from: www.sean-barton.co.uk @ 2012-02-08 23:19:03 -->
