<?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>Homepage of Sean Barton, a freelance website developer from Cheshire</description>
	<lastBuildDate>Sat, 19 Jun 2010 12:24:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 [...]]]></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>
