<?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>Kohalza&#039;s Blog</title> <atom:link href="http://www.kohalza.com/feed/" rel="self" type="application/rss+xml" /><link>http://www.kohalza.com</link> <description>Front-End Web Development</description> <lastBuildDate>Sat, 05 Feb 2011 21:32:46 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.5</generator> <item><title>Javascript number formatting</title><link>http://www.kohalza.com/posts/javascript-number-formatting/</link> <comments>http://www.kohalza.com/posts/javascript-number-formatting/#comments</comments> <pubDate>Tue, 01 Feb 2011 14:18:45 +0000</pubDate> <dc:creator>Kohalza</dc:creator> <category><![CDATA[Javascript]]></category><guid
isPermaLink="false">http://www.kohalza.com/?p=9</guid> <description><![CDATA[I often find myself in need to display different numbers and stats in my apps. Many times these numbers are either calculated in Javascript or are retrieved via AJAX as JSON data. The problem is that Javascript doesn&#8217;t supply much in the way of number formatting. This is why I wrote this small utility function [...]]]></description> <content:encoded><![CDATA[<p>I often find myself in need to display different numbers and stats in my apps. Many times these numbers are either calculated in Javascript or are retrieved via <acronym
title="Asynchronous JavaScript and XML">AJAX</acronym> as <acronym
title="Javascript Object Notation">JSON</acronym> data.<br
/> The problem is that Javascript doesn&#8217;t supply much in the way of number formatting. This is why I wrote this small utility function to display big numbers with thousand separators. This function handles well any number, either integer or float, positive and negative values.<br
/> It receives one parameter that controls the numbers of digits to show after the decimal point, which uses the build-in <a
href="http://www.w3schools.com/jsref/jsref_tofixed.asp">toFixed</a> method. Note that this also means that the number will be rounded to that decimal point.<br
/> I&#8217;m posting 2 variations of the function, so you can use whichever of the 2 that you&#8217;re more comfortable with.<br
/> The prototype version:</p><pre name="code" class="javascript">Number.prototype.toCommaSeparated = function(decimal){
	var parts = Math.abs(this).toFixed(decimal || 0).split('.');
	return (this &lt; 0 ? '-' : '') + parts[0].split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(/,$/, '').split('').reverse().join('') + (parts[1] ? '.'+parts[1] : '');
};</pre><p>usage example:</p><pre name="code" class="javascript">
var bigNumber = 1234567;
bigNumber.toCommaSeparated();
</pre><p>The functional version:</p><pre name="code" class="javascript">function toCommaSeparated(num, decimal){
	var parts = Math.abs(num).toFixed(decimal || 0).split('.');
	return (num &lt; 0 ? '-' : '') + parts[0].split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(/,$/, '').split('').reverse().join('') + (parts[1] ? '.'+parts[1] : '');
};</pre><p>usage example:</p><pre name="code" class="javascript">
var bigNumber = 1234567;
toCommaSeparated(bigNumber);
</pre><p>Enjoy!</p> <iframe
src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.kohalza.com%2Fposts%2Fjavascript-number-formatting%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded> <wfw:commentRss>http://www.kohalza.com/posts/javascript-number-formatting/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk
Object Caching 174/189 objects using disk

Served from: www.kohalza.com @ 2012-05-20 16:59:00 -->
