<?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>Xaprb &#187; formatting</title>
	<atom:link href="http://www.xaprb.com/blog/tag/formatting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xaprb.com/blog</link>
	<description>Stay curious!</description>
	<lastBuildDate>Thu, 09 Feb 2012 03:58:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>JavaScript Number Formatting Library v1.3 released</title>
		<link>http://www.xaprb.com/blog/2007/07/15/javascript-number-formatting-library-v13-released/</link>
		<comments>http://www.xaprb.com/blog/2007/07/15/javascript-number-formatting-library-v13-released/#comments</comments>
		<pubDate>Sun, 15 Jul 2007 13:52:07 +0000</pubDate>
		<dc:creator>Xaprb</dc:creator>
				<category><![CDATA[format strings]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[number formatting]]></category>
		<category><![CDATA[numberformat]]></category>
		<category><![CDATA[parsing]]></category>

		<guid isPermaLink="false">http://www.xaprb.com/blog/2007/07/15/javascript-number-formatting-library-v13-released/</guid>
		<description><![CDATA[<p>I've updated my JavaScript Number Formatting Library to version 1.3.  This release adds the ability to customize how not-a-number (NaN), positive infinity and negative infinity are formatted.  All you need to do is set the appropriate constant in Number.prototype.</p>


<strong>Further Reading:</strong><ul><li><a href='http://www.xaprb.com/blog/2007/06/19/javascript-number-formatting-library-updated/' rel='bookmark' title='Permanent Link: JavaScript number-formatting library updated'>JavaScript number-formatting library updated</a></li>
<li><a href='http://www.xaprb.com/blog/2007/06/25/javascript-formatting-library-update/' rel='bookmark' title='Permanent Link: JavaScript formatting library update'>JavaScript formatting library update</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency/' rel='bookmark' title='Permanent Link: JavaScript date parsing and formatting, Part 1'>JavaScript date parsing and formatting, Part 1</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/20/javascript-date-parsing/' rel='bookmark' title='Permanent Link: Javascript date parsing and formatting, Part 2'>Javascript date parsing and formatting, Part 2</a></li>
<li><a href='http://www.xaprb.com/blog/2006/05/14/javascript-date-formatting-benchmarks/' rel='bookmark' title='Permanent Link: JavaScript date formatting benchmarks'>JavaScript date formatting benchmarks</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p class="download"><a href="/articles/number-functions.zip">Download Number Formatting Library</a></p>

<p>I&#8217;ve updated my JavaScript Number Formatting Library to version 1.3.  This release adds the ability to customize how not-a-number (NaN), positive infinity and negative infinity are formatted.  All you need to do is set the appropriate constant in <code>Number.prototype</code>:</p>

<ul>
<li><code>Number.prototype.NaN</code></li>
<li><code>Number.prototype.posInfinity</code></li>
<li><code>Number.prototype.negInfinity</code></li>
</ul>

<p>For more documentation, see the <a href="/blog/2006/01/05/javascript-number-formatting/">original article on JavaScript number formatting</a>.</p>

<p><strong>Further Reading:</strong><ul><li><a href='http://www.xaprb.com/blog/2007/06/19/javascript-number-formatting-library-updated/' rel='bookmark' title='Permanent Link: JavaScript number-formatting library updated'>JavaScript number-formatting library updated</a></li>
<li><a href='http://www.xaprb.com/blog/2007/06/25/javascript-formatting-library-update/' rel='bookmark' title='Permanent Link: JavaScript formatting library update'>JavaScript formatting library update</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency/' rel='bookmark' title='Permanent Link: JavaScript date parsing and formatting, Part 1'>JavaScript date parsing and formatting, Part 1</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/20/javascript-date-parsing/' rel='bookmark' title='Permanent Link: Javascript date parsing and formatting, Part 2'>Javascript date parsing and formatting, Part 2</a></li>
<li><a href='http://www.xaprb.com/blog/2006/05/14/javascript-date-formatting-benchmarks/' rel='bookmark' title='Permanent Link: JavaScript date formatting benchmarks'>JavaScript date formatting benchmarks</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.xaprb.com/blog/2007/07/15/javascript-number-formatting-library-v13-released/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>JavaScript number-formatting library updated</title>
		<link>http://www.xaprb.com/blog/2007/06/19/javascript-number-formatting-library-updated/</link>
		<comments>http://www.xaprb.com/blog/2007/06/19/javascript-number-formatting-library-updated/#comments</comments>
		<pubDate>Wed, 20 Jun 2007 02:13:50 +0000</pubDate>
		<dc:creator>Xaprb</dc:creator>
				<category><![CDATA[formatting]]></category>
		<category><![CDATA[format_strings]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[number formatting]]></category>
		<category><![CDATA[numberformat]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.xaprb.com/blog/2007/06/19/javascript-number-formatting-library-updated/</guid>
		<description><![CDATA[<p>I've released a new version of my <a href="/blog/2006/01/05/javascript-number-formatting/">powerful, flexible, efficient JavaScript number-formatting library</a>, which is probably the best available. This release adds a fix for zero-padding negative numbers.</p>


<strong>Further Reading:</strong><ul><li><a href='http://www.xaprb.com/blog/2007/07/15/javascript-number-formatting-library-v13-released/' rel='bookmark' title='Permanent Link: JavaScript Number Formatting Library v1.3 released'>JavaScript Number Formatting Library v1.3 released</a></li>
<li><a href='http://www.xaprb.com/blog/2007/06/25/javascript-formatting-library-update/' rel='bookmark' title='Permanent Link: JavaScript formatting library update'>JavaScript formatting library update</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/20/javascript-date-parsing/' rel='bookmark' title='Permanent Link: Javascript date parsing and formatting, Part 2'>Javascript date parsing and formatting, Part 2</a></li>
<li><a href='http://www.xaprb.com/blog/2006/05/14/javascript-date-formatting-benchmarks/' rel='bookmark' title='Permanent Link: JavaScript date formatting benchmarks'>JavaScript date formatting benchmarks</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency/' rel='bookmark' title='Permanent Link: JavaScript date parsing and formatting, Part 1'>JavaScript date parsing and formatting, Part 1</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p class="download"><a href="/articles/number-functions.zip">Download number-functions</a></p>

<p>I&#8217;ve released a new version of my <a href="/blog/2006/01/05/javascript-number-formatting/">powerful, flexible, efficient JavaScript number-formatting library</a>, which is probably the best available. This release adds a fix for zero-padding negative numbers.</p>

<p>If you find bugs, please send me test cases I can use to reproduce and add to the unit test suite.  One test per line, like &#8220;input&#8221;, &#8220;format&#8221;, &#8220;expected&#8221; is best.  For example, this is a great test case:</p>

<pre>-1, "#,#.00", "-1.00"</pre>

<p>I can plug that directly into the unit test suite, run it, and if it gives back &#8220;-01.00&#8243; it will fail the test.  This makes it much easier and more convenient for me to fix bugs.</p>

<p><a href="/blog/donate/">Sponsoring</a> bug fixes wouldn&#8217;t hurt either ;-)</p>

<p><strong>Further Reading:</strong><ul><li><a href='http://www.xaprb.com/blog/2007/07/15/javascript-number-formatting-library-v13-released/' rel='bookmark' title='Permanent Link: JavaScript Number Formatting Library v1.3 released'>JavaScript Number Formatting Library v1.3 released</a></li>
<li><a href='http://www.xaprb.com/blog/2007/06/25/javascript-formatting-library-update/' rel='bookmark' title='Permanent Link: JavaScript formatting library update'>JavaScript formatting library update</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/20/javascript-date-parsing/' rel='bookmark' title='Permanent Link: Javascript date parsing and formatting, Part 2'>Javascript date parsing and formatting, Part 2</a></li>
<li><a href='http://www.xaprb.com/blog/2006/05/14/javascript-date-formatting-benchmarks/' rel='bookmark' title='Permanent Link: JavaScript date formatting benchmarks'>JavaScript date formatting benchmarks</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency/' rel='bookmark' title='Permanent Link: JavaScript date parsing and formatting, Part 1'>JavaScript date parsing and formatting, Part 1</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.xaprb.com/blog/2007/06/19/javascript-number-formatting-library-updated/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to format numbers in JavaScript flexibly and efficiently</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/</link>
		<comments>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comments</comments>
		<pubDate>Fri, 06 Jan 2006 00:22:03 +0000</pubDate>
		<dc:creator>Xaprb</dc:creator>
				<category><![CDATA[formatting]]></category>
		<category><![CDATA[format_strings]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[numberformat]]></category>
		<category><![CDATA[parsing]]></category>

		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=74</guid>
		<description><![CDATA[<p>This article continues my series on parsing and formatting data with JavaScript, this time with numeric data.  I don't need to do number <em>parsing</em>, but <em>formatting</em> is very useful.  The technique is similar to my date formatting code -- code that writes code, using custom format specifier strings.  The result is number formatting functionality I hope will be familiar and easy to use.</p>


<strong>Further Reading:</strong><ul><li><a href='http://www.xaprb.com/blog/2007/07/15/javascript-number-formatting-library-v13-released/' rel='bookmark' title='Permanent Link: JavaScript Number Formatting Library v1.3 released'>JavaScript Number Formatting Library v1.3 released</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/20/javascript-date-parsing/' rel='bookmark' title='Permanent Link: Javascript date parsing and formatting, Part 2'>Javascript date parsing and formatting, Part 2</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency/' rel='bookmark' title='Permanent Link: JavaScript date parsing and formatting, Part 1'>JavaScript date parsing and formatting, Part 1</a></li>
<li><a href='http://www.xaprb.com/blog/2007/06/19/javascript-number-formatting-library-updated/' rel='bookmark' title='Permanent Link: JavaScript number-formatting library updated'>JavaScript number-formatting library updated</a></li>
<li><a href='http://www.xaprb.com/blog/2007/06/25/javascript-formatting-library-update/' rel='bookmark' title='Permanent Link: JavaScript formatting library update'>JavaScript formatting library update</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p style="border:solid red 1px; background:yellow">If you have questions or comments or bugs report, or a change to make, be sure to use the project&#8217;s new homepage: <a href="http://code.google.com/p/flexible-js-formatting/">Flexible JS Formatting Libraries</a></p>

<p class="download"><a href="/articles/number-functions.zip">Download number-functions</a></p>

<p>This article continues my series on parsing and formatting data with JavaScript, this time with numeric data.  I don&#8217;t need to do number <em>parsing</em>, but <em>formatting</em> is very useful.  The technique is similar to my <a href="/blog/2005/12/12/javascript-closures-for-runtime-efficiency/">date formatting</a> code &#8212; code that writes code (for raw speed), using custom format specifier strings (for flexibility and ease of use).  The result is number formatting functionality that is highly efficient, flexible, and easy to use.</p>

<p>First, the idea: you have a number, you want it formatted a certain way.  Here&#8217;s how:</p>

<pre>var dollars = 5.001;
alert(dollars.numberFormat("$0.00");
// result: "$5.00"
var percent = .08134;
alert(percent.numberFormat("0.00%");
// result: "8.13%"
var bignum = 12831242485472;
alert(bignum.numberFormat("0,0,, million");
// result: "12,831,243 million"</pre>

<p>My custom date formatting code used PHP&#8217;s date-formatting syntax because it&#8217;s much less context-sensitive and (I think) more useful than Microsoft&#8217;s, but my number-formatting syntax is similar to Microsoft&#8217;s because it&#8217;s much more widely used and I don&#8217;t see an existing, better alternative.  Rather than documenting it separately, I&#8217;ll just point you to the (<a href="/blog/2005/12/30/excel-calc-number-formatting/">poor quality</a>) Microsoft documentation for the <a href="http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcustomnumericformatstrings.asp">.NET Custom Numeric Format Strings</a> functionality, and list the differences from my implementation:</p>

<ul>
	<li>Rounding works differently in multi-section format strings.  In .NET with a two-section string, <blockquote><p>If the number to be formatted is negative, but becomes zero after rounding according to the format in the second section, then the resulting zero is formatted according to the first section.</p></blockquote>  This is not true in my code &#8212; the number is formatted according to its value, and once the code decides which section applies, that section will be used no matter what happens during rounding.</li>
	<li>Question marks are digit placeholders just like the number sign (<code>#</code>), but if there&#8217;s no digit to insert, they get replaced with spaces, not removed.  They can be used for space-padding, which might be useful for, say, accounting notation.</li>
	<li>You don&#8217;t have to enter quotes around strings that should be mixed in with the number placeholders.  In fact, my syntax is much more permissive than the Microsoft syntax: anything can go anywhere.  You can put arbitrary strings smack in the middle of your number if you want.</li>
	<li>It&#8217;s not internationalized.</li>
</ul>

<p>I&#8217;ve only implemented a subset of the various number-formatting syntaxes I&#8217;ve seen in spreadsheets and so forth.  The subset is about 85% complete in my opinion.  However, I think it&#8217;s <em>functionally</em> about 99% complete, which means I think 99% of the time you want to format a number, it will do what you want.  The tradeoff is simplicity and speed.  Number formatting is actually much more difficult than date formatting, and I&#8217;ve tried to keep the code sane.</p>

<p>I have a set of unit tests, which use the excellent <a href="http://www.edwardh.com/jsunit/">JsUnit</a> library.  Bring up the <a href="/jsunit/testRunner.html">unit test page</a> and enter the following url to be tested: <code>www.xaprb.com/articles/number-test.html</code>.</p>

<p>Of course there&#8217;s the obligatory <a href="/articles/number-formatting-demo.html">demo page</a>, too.</p>

<p><strong>Further Reading:</strong><ul><li><a href='http://www.xaprb.com/blog/2007/07/15/javascript-number-formatting-library-v13-released/' rel='bookmark' title='Permanent Link: JavaScript Number Formatting Library v1.3 released'>JavaScript Number Formatting Library v1.3 released</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/20/javascript-date-parsing/' rel='bookmark' title='Permanent Link: Javascript date parsing and formatting, Part 2'>Javascript date parsing and formatting, Part 2</a></li>
<li><a href='http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency/' rel='bookmark' title='Permanent Link: JavaScript date parsing and formatting, Part 1'>JavaScript date parsing and formatting, Part 1</a></li>
<li><a href='http://www.xaprb.com/blog/2007/06/19/javascript-number-formatting-library-updated/' rel='bookmark' title='Permanent Link: JavaScript number-formatting library updated'>JavaScript number-formatting library updated</a></li>
<li><a href='http://www.xaprb.com/blog/2007/06/25/javascript-formatting-library-update/' rel='bookmark' title='Permanent Link: JavaScript formatting library update'>JavaScript formatting library update</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
	</channel>
</rss>

