<?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>yesJames.com &#187; Software</title>
	<atom:link href="http://www.yesjames.com/index.php/tag/software/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.yesjames.com</link>
	<description>ahuh... sure... what ever you say...</description>
	<lastBuildDate>Fri, 21 Oct 2011 05:16:18 +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>howto: Format numbers in JavaScript</title>
		<link>http://www.yesjames.com/index.php/2008/11/howto-format-numbers-in-javascript/</link>
		<comments>http://www.yesjames.com/index.php/2008/11/howto-format-numbers-in-javascript/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 23:17:32 +0000</pubDate>
		<dc:creator>james</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[web programming]]></category>

		<guid isPermaLink="false">http://www.yesjames.com/?p=41</guid>
		<description><![CDATA[Formatting Numbers Numeric formatting in JavaScript can be very tedious. From handling non-numeric characters, to multiple-currency symbols, to alternate negative display methods. The functions below should be valuable in assisting to format numbers and currencies of all types. To do complete formatting of a number, the following function formatNumber(), takes several parameters specifying the properties [...]]]></description>
			<content:encoded><![CDATA[<h3>Formatting Numbers</h3>
<p>Numeric formatting in JavaScript can be very tedious. From handling non-numeric characters, to multiple-currency symbols, to alternate negative display methods. The functions below should be valuable in assisting to format numbers and currencies of all types.</p>
<p><span id="more-41"></span></p>
<p>To do complete formatting of a number, the following function <b>formatNumber()</b>, takes several parameters specifying the properties of the output.</p>
<pre name="code" class="brush: js">
/* formatNumber: PARAMETERS...
num: the decimal number to convert (must be numeric)
dec: number of resulting decimal places to keep
thou: the character to use for thousands separator
pnt: the character to use for decimal point
curr1: Preceding Currency Symbol
curr2: Trailing Currency symbol
n1: Preceding Negative character
n2: Trailing Negative character
*/
function formatNumber(num, dec, thou, pnt, curr1, curr2, n1, n2) {
	var x = Math.round(num * Math.pow(10,dec));
	if (x >= 0) n1 = n2 = '';
	var y = (''+ Math.abs(x)).split('');
	var z = y.length - dec;
	if (z<0) z--;
	for (var i = z; i < 0; i++) y.unshift('0');
	if (z<0) z = 1;
	y.splice(z, 0, pnt);
	if (y[0] == pnt) y.unshift('0');
	while (z > 3) { z-=3; y.splice(z,0,thou); }
	var r = curr1 + n1 + y.join('') + n2 + curr2;
	return r;
}
</pre>
<p>To use this function, simply call the function with the parameters you require.<br />
e.g. To display a currency (western such as USD or AUD):</p>
<pre name="code" class="brush: js">
var cur = formatNumber(1257.45, 2, ',', '.', '$', '', '-', '');
alert(cur);
</pre>
<p>will output: <b>$1,257.45</b></p>
<p>Alternatively, you may decide to use a helper function that defaults most of these values for you. For example:</p>
<pre name="code" class="brush: js">
function formatCurrency(num) {
  return formatNumber(num, 2, ',', '.', '$', '', '-', '');
}
</pre>
<p>The thousandsFormat() function takes any decimal number, negative or positive, and formats it by adding commas every three digits. This makes the number more readable for your site visitors:</p>
<pre name="code" class="brush: js">
// This function formats numbers by adding commas
function thousandsFormat(nStr) {
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length &gt; 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1))
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  return x1 + x2;
}
</pre>
<h2>Strip Non-Numeric Characters From a String</h2>
<p>The stripNonNumeric() function strips any non-numeric characters from a string leaving you with a valid decimal number. This function considers the minus sign (hyphen) and the period to be numeric and will not strip them unless the minus sign is not at the beginning of the number or there is more than one period:</p>
<pre name="code" class="brush: js">
// This function removes non-numeric characters
function stripNonNumeric(str) {
  str += '';
  var rgx = /^\d|\.|-$/;
  var out = '';
  for (var i = 0; i < str.length; i++) {
    if (rgx.test(str.charAt(i))) {
      if (!((str.charAt(i)=='.' &#038;&#038; out.indexOf('.')!=-1)
        || (str.charAt(i)=='-' &#038;&#038;  out.length!=0))) {
        out += str.charAt(i);
      }
    }
  }
  return out;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.yesjames.com/index.php/2008/11/howto-format-numbers-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

