<?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>DansNetwork.com &#187; Financial</title>
	<atom:link href="http://dansnetwork.com/category/financial/feed/" rel="self" type="application/rss+xml" />
	<link>http://dansnetwork.com</link>
	<description>Web Design, Javascript, CSS, and More...</description>
	<lastBuildDate>Sun, 29 Apr 2012 11:36:29 +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>Round Half To Even With Javascript</title>
		<link>http://dansnetwork.com/round-half-to-even-with-javascript/</link>
		<comments>http://dansnetwork.com/round-half-to-even-with-javascript/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 02:19:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Financial]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://dansnetwork.com/?p=69</guid>
		<description><![CDATA[Updated: v0.2.1 
(special thanks to: Donatas v0.2.0 (http://happycode.info)
The following script provides an easy way to perform unbiased rounding, convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, or bankers' rounding using Javascript.
Examples
var numToRound = 10.005;
numToRound.rhte(0.01);
result: 10.00
var numToRound = 10.0015;
numToRound.rhte(0.001);
result: 10.002
var numToRound = 10.00500000001;
numToRound.rhte(0.01);
result: 10.01
var numToRound = 1005001;
numToRound.rhte(10000);
result: 1010000&#160;

&#60;script&#62;

/*
  Round Half To Even (rhte) extends the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Updated: v0.2.1 </p>
<p>(special thanks to: Donatas v0.2.0 (<a href="http://happycode.info">http://happycode.info</a>)</strong></p>
<p>The following script provides an easy way to perform unbiased rounding, convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, or bankers' rounding using Javascript.</p>
<p><span style="text-decoration: underline;">Examples</span></p>
<p><em>var numToRound = 10.005;<br />
numToRound.rhte(0.01);<br />
result: <span style="text-decoration: underline;">10.00</span></em></p>
<p><strong>var numToRound = 10.0015;<br />
numToRound.rhte(0.001);<br />
result: <span style="text-decoration: underline;">10.002</span></strong></p>
<p><em>var numToRound = 10.00500000001;<br />
numToRound.rhte(0.01);<br />
result: <span style="text-decoration: underline;">10.01</span></em></p>
<p><strong>var numToRound = 1005001;<br />
numToRound.rhte(10000);<br />
result: <span style="text-decoration: underline;">1010000</span></strong><br />&nbsp;</p>
<div class="codenplay">
&lt;script&gt;
<textarea class="cpJS">
/*
  Round Half To Even (rhte) extends the 'Number' and 'String' objects
  Argument(placeToRound) examples: 0.0001,0.01,1,10,100
  Usage:
    Number.rhte(rounding value)
      (124.34450000).rhte(0.001) - [Result: 124.344]
      (124.34450001).rhte(0.001) - [Result: 124.345]
      (124.34550000).rhte(0.001) - [Result: 124.346]
    String.rhte(rounding value)
      '124.34450000'.rhte(0.001) - [Result: 124.344]
      '124.34450001'.rhte(0.001) - [Result: 124.345]
      '124.34550000'.rhte(0.001) - [Result: 124.346]
*/
var rhte = function(placeToRound){
  var fixed = placeToRound.toString().split('.').length < 2 ? 0 : placeToRound.toString().split('.')[1].length,
  numParts = {
    mvDec : (this/placeToRound).toFixed(this.toString().length).toString().split('.'),
    wholeNum: function(){return parseInt(this.mvDec[0],10);},
    dec: function(){return this.mvDec.length > 1 ? parseFloat('0.'+this.mvDec[1]) : 0;},
    oddEven: function(){return (this.wholeNum() % 2 === 1) ? 1 : 0;}
  };
      
  if(numParts.dec() !== 0.5){
    return(numParts.dec() > 0.5) ? parseFloat(((numParts.wholeNum()+1)*placeToRound).toFixed(fixed)) : parseFloat((numParts.wholeNum()*placeToRound).toFixed(fixed));
  }
  else{
    if(numParts.oddEven() === 1){
      return parseFloat(((numParts.wholeNum()+1)*placeToRound).toFixed(fixed));
    }
    else{
      return parseFloat((numParts.wholeNum()*placeToRound).toFixed(fixed));
    }
  }
};
Number.prototype.rhte = rhte;
String.prototype.rhte = rhte;

test("Unit Tests", function() { //QUnit Tests
  ok( (1.000).rhte(1) === 1, "(1.000).rhte(1) === 1" );
  ok( ('1.000').rhte(1) === 1, "('1.000').rhte(1) === 1" );
  ok( (1.01).rhte(0.001) === 1.010, "(1.01).rhte(0.001) === 1.010" );
  ok( (10.0501).rhte(0.1) === 10.1, "(10.0501).rhte(0.1) === 10.1" );
  ok( (1.01499999).rhte("0.01") === 1.01, "(1.01499999).rhte(\"0.01\") === 1.01" );
  ok( (1.0150000000).rhte(0.01) === 1.02, "(1.0150000000).rhte(0.01) === 1.02" );
  ok( (476).rhte(50) === 500, "(476).rhte(50) === 500" );
});

</textarea>
&lt;html&gt;
<textarea class="cpHTML">
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<link rel="stylesheet" href="https://raw.github.com/jquery/qunit/master/qunit/qunit.css" type="text/css" />
<script type="text/javascript" src="https://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<meta charset=utf-8 />

<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup,
  menu, nav, section { display: block; }
</style>
</head>
<body>
   <h1 id="qunit-header">QUnit Tests</h1>
   <h2 id="qunit-banner"></h2>
   <div id="qunit-testrunner-toolbar"></div>
   <h2 id="qunit-userAgent"></h2>
   <ol id="qunit-tests"></ol>
   <div id="qunit-fixture"></div>
</body>
</html>

</textarea>Result:<br /><iframe src="http://dansnetwork.com/wp-content/plugins/codenplay/index.html" class="cpResult run"></iframe><br /><a class="cpRun" title="Run" href="#run">Run</a></div><script type="text/javascript">var codenplay={}; codenplay.WP_PLUGIN_URL = "http://dansnetwork.com/wp-content/plugins";</script><script type="text/javascript" src="http://dansnetwork.com/wp-content/plugins/codenplay/js/LAB.min.js"></script><link rel="stylesheet" type="text/css" href="http://dansnetwork.com/wp-content/plugins/codenplay/css/style.css" />]]></content:encoded>
			<wfw:commentRss>http://dansnetwork.com/round-half-to-even-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

