<?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>WLScripting.com &#187; Math</title>
	<atom:link href="http://www.wlscripting.com/tutorial/category/math/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wlscripting.com</link>
	<description>PHP coding tutorials</description>
	<lastBuildDate>Mon, 19 Dec 2011 13:17:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9-beta-1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Calculate a percentage</title>
		<link>http://www.wlscripting.com/tutorial/57</link>
		<comments>http://www.wlscripting.com/tutorial/57#comments</comments>
		<pubDate>Tue, 24 Mar 2009 16:59:25 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://www.wlscripting.com/tutorial/57</guid>
		<description><![CDATA[Function for simplifying percentage calculations given a numerator and denominator]]></description>
			<content:encoded><![CDATA[<p>Below is a quick function that simplifies the process of calculating a percentage in PHP.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">function </span><span class="hl-identifier">percent</span><span class="hl-brackets">(</span><span class="hl-var">$Amount</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-var">$Total</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-var">$Decimal</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-brackets">) {
	</span><span class="hl-comment">// Make sure our numbers are actually numbers
	</span><span class="hl-var">$Amount</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">INT</span><span class="hl-brackets">) </span><span class="hl-var">$Amount</span><span class="hl-code">;
	</span><span class="hl-var">$Total</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">INT</span><span class="hl-brackets">) </span><span class="hl-var">$Total</span><span class="hl-code">;
	</span><span class="hl-var">$Decimal</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">INT</span><span class="hl-brackets">) </span><span class="hl-var">$Decimal</span><span class="hl-code">;
	</span><span class="hl-comment">// Cannot divide by zero so check if Total = 0
	</span><span class="hl-reserved">if </span><span class="hl-brackets">(</span><span class="hl-var">$Total</span><span class="hl-code"> === </span><span class="hl-number">0</span><span class="hl-brackets">) {
		</span><span class="hl-reserved">return </span><span class="hl-number">0</span><span class="hl-code">;
	</span><span class="hl-brackets">}
	</span><span class="hl-var">$p</span><span class="hl-code"> = </span><span class="hl-var">$Amount</span><span class="hl-code"> / </span><span class="hl-var">$Total</span><span class="hl-code">;
	</span><span class="hl-comment">// Multiply by 100 to move decimal point to correct location
	</span><span class="hl-var">$p</span><span class="hl-code"> *= </span><span class="hl-number">100</span><span class="hl-code">;
	</span><span class="hl-comment">// Return the percentage with specified decimal places
	</span><span class="hl-reserved">return </span><span class="hl-identifier">number_format</span><span class="hl-brackets">(</span><span class="hl-var">$p</span><span class="hl-code">, </span><span class="hl-var">$Decimal</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Then to use this code you can do:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-comment">// prints out 10% on the screen
</span><span class="hl-reserved">echo </span><span class="hl-identifier">percent</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">,</span><span class="hl-quotes">'</span><span class="hl-string">%</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/57/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add suffix to a number</title>
		<link>http://www.wlscripting.com/tutorial/56</link>
		<comments>http://www.wlscripting.com/tutorial/56#comments</comments>
		<pubDate>Tue, 24 Mar 2009 16:03:01 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://www.wlscripting.com/tutorial/56</guid>
		<description><![CDATA[Add st, nd, rd and th suffix to numbers dynamically]]></description>
			<content:encoded><![CDATA[<p>Have you ever had to dynamically add the suffix to a number? (ie. st, nd, rd, th)</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">function </span><span class="hl-identifier">number_suffix</span><span class="hl-brackets">(</span><span class="hl-var">$i</span><span class="hl-brackets">) {
    </span><span class="hl-reserved">switch</span><span class="hl-brackets">( </span><span class="hl-identifier">floor</span><span class="hl-brackets">(</span><span class="hl-var">$i</span><span class="hl-code">/</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code"> % </span><span class="hl-number">10 </span><span class="hl-brackets">) {
        </span><span class="hl-reserved">default</span><span class="hl-code">:
	    </span><span class="hl-reserved">switch</span><span class="hl-brackets">( </span><span class="hl-var">$i</span><span class="hl-code"> % </span><span class="hl-number">10 </span><span class="hl-brackets">) {
                </span><span class="hl-reserved">case </span><span class="hl-number">1</span><span class="hl-code">: </span><span class="hl-reserved">return </span><span class="hl-quotes">'</span><span class="hl-string">st</span><span class="hl-quotes">'</span><span class="hl-code">;
                </span><span class="hl-reserved">case </span><span class="hl-number">2</span><span class="hl-code">: </span><span class="hl-reserved">return </span><span class="hl-quotes">'</span><span class="hl-string">nd</span><span class="hl-quotes">'</span><span class="hl-code">;
                </span><span class="hl-reserved">case </span><span class="hl-number">3</span><span class="hl-code">: </span><span class="hl-reserved">return </span><span class="hl-quotes">'</span><span class="hl-string">rd</span><span class="hl-quotes">'</span><span class="hl-code">;  
            </span><span class="hl-brackets">}
        </span><span class="hl-reserved">case </span><span class="hl-number">1</span><span class="hl-code">:
    </span><span class="hl-brackets">}
    </span><span class="hl-reserved">return </span><span class="hl-quotes">'</span><span class="hl-string">th</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-brackets">}
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Then to use the code you would do like:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-var">$number</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
</span><span class="hl-comment">// This will display 10th
</span><span class="hl-reserved">echo </span><span class="hl-var">$number</span><span class="hl-code">,</span><span class="hl-identifier">number_suffix</span><span class="hl-brackets">(</span><span class="hl-var">$number</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/56/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Zero fill a number</title>
		<link>http://www.wlscripting.com/tutorial/55</link>
		<comments>http://www.wlscripting.com/tutorial/55#comments</comments>
		<pubDate>Mon, 16 Mar 2009 14:42:46 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://www.wlscripting.com/tutorial/55</guid>
		<description><![CDATA[Add zeros to the beginning of a number to make output number a certain length.]]></description>
			<content:encoded><![CDATA[<blockquote style="border: 2px solid #FF0000; text-align: center"><p>An updated posting on this subject can be found at:<br /><a href="http://www.danielkassner.com/2010/05/19/working-with-zero-filled-numbers-in-php">Working with zero filled numbers in PHP</a></p></blockquote>
<p>Have you ever needed to make sure your number is a certain length for output? Say you have the number 5 but need to make sure it is 3 digits and out put like: 005. How would you go about this? One way to do this is the following:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-reserved">function </span><span class="hl-identifier">zerofill </span><span class="hl-brackets">(</span><span class="hl-var">$num</span><span class="hl-code">,</span><span class="hl-var">$zerofill</span><span class="hl-brackets">) {
    </span><span class="hl-comment">// Get the current string length of the original number
    // Loop through that number until it has reached the count in $zerofill
    </span><span class="hl-reserved">while </span><span class="hl-brackets">(</span><span class="hl-identifier">strlen</span><span class="hl-brackets">(</span><span class="hl-var">$num</span><span class="hl-brackets">)</span><span class="hl-code">&lt;</span><span class="hl-var">$zerofill</span><span class="hl-brackets">) {
        </span><span class="hl-var">$num</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">0</span><span class="hl-quotes">&quot;</span><span class="hl-code">.</span><span class="hl-var">$num</span><span class="hl-code">;
    </span><span class="hl-brackets">}
    </span><span class="hl-reserved">return </span><span class="hl-var">$num</span><span class="hl-code">;
</span><span class="hl-brackets">}
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>The usage of this code would be:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
</span><span class="hl-comment">// Output will be 005
</span><span class="hl-reserved">echo </span><span class="hl-identifier">zerofill</span><span class="hl-brackets">(</span><span class="hl-number">5</span><span class="hl-code">, </span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wlscripting.com/tutorial/55/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

