<?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>Jamie&#039;s Blog &#187; rounding</title>
	<atom:link href="http://www.angelforge.org/wordpress/tags/rounding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.angelforge.org/wordpress</link>
	<description>My life is words.</description>
	<lastBuildDate>Sat, 28 Jan 2012 04:35:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Javascript Gaussian/Banker&#8217;s Rounding</title>
		<link>http://www.angelforge.org/wordpress/programming/javascript-gaussianbankers-rounding/</link>
		<comments>http://www.angelforge.org/wordpress/programming/javascript-gaussianbankers-rounding/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 17:34:31 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[rounding]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://www.angelforge.org/wordpress/?p=1303</guid>
		<description><![CDATA[Here&#8217;s a function for Gaussian/Banker&#8217;s Rounding in Javascript adapted from code written by Michael Boon at http://boonedocks.net/. This can be useful if you&#8217;re working with Microsoft languages such as VBScript, which use Banker&#8217;s Rounding by default in their Round function. Javascript has no built-in gaussian rounding and, instead, uses Arithmetic rounding. For more information on [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a function for Gaussian/Banker&#8217;s Rounding in Javascript adapted from code written by Michael Boon at <a href="http://boonedocks.net/">http://boonedocks.net/</a>.</p>
<p>This can be useful if you&#8217;re working with Microsoft languages such as VBScript, which use Banker&#8217;s Rounding by default in their Round function. Javascript has no built-in gaussian rounding and, instead, uses Arithmetic rounding. For more information on this, see <a href="http://en.wikipedia.org/wiki/Rounding#Round-to-even_method">Wikipedia&#8217;s article on rounding</a>, specifically, the Round to Even section.</p>
<pre class="brush: jscript; title: ; notranslate">
/*
    Adapted from &lt;a href=&quot;http://boonedocks.net/code/bround.inc.phps&quot;&gt;http://boonedocks.net/code/bround.inc.phps&lt;/a&gt;
Provided under the GNU General Public License
    Contact me for use outside the bounds of that license

    ---------------------------------------------------------------
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    The GNU General Public License can be found at:

http://www.gnu.org/copyleft/gpl.html

*/
&lt;a href=&quot;http://boonedocks.net/code/bround.inc.phps&quot;&gt;&lt;/a&gt;
Number.prototype.gaussianRound = Number.prototype.bankersRound = function bround(iDec) {
    return Math.gaussianRound ( this, iDec );
};

Math.gaussianRound = Math.bankersRound = function (dVal, iDec) {

    // banker's style rounding or round-half-even
    // (round down when even number is left of 5, otherwise round up)
    // dVal is value to round
    // iDec specifies number of decimal places to retain

    var
		dFuzz=0.00001, // to deal with floating-point precision loss
		iRoundup=0, // amount to round up by
		iSign= dVal != 0.0 ? Math.floor ( dVal/ Math.abs( dVal ) ) : 1;

	dVal=Math.abs(dVal);

    // get decimal digit in question and amount to right of it as a fraction
    dWorking = dVal * Math.pow ( 10.0, iDec + 1 ) -
		Math.floor ( dVal * Math.pow ( 10.0, iDec ) ) * 10.0;

	iEvenOddDigit =
		Math.floor ( dVal * Math.pow ( 10.0, iDec) ) -
		Math.floor ( dVal * Math.pow ( 10.0, iDec-1 ) ) * 10.0;

    if ( Math.abs ( dWorking - 5.0 ) &amp;lt; dFuzz)
		iRoundup= iEvenOddDigit &amp;amp; 1 ? 1 : 0; // even testing using bitwise and
    else
		iRoundup= dWorking &amp;gt; 5.0 ? 1 : 0;

    return iSign * ( ( Math.floor ( dVal * Math.pow (10.0,iDec ) ) + iRoundup )/ Math.pow(10.0,iDec) );
};
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.angelforge.org/wordpress/programming/javascript-gaussianbankers-rounding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

