<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to format numbers in JavaScript flexibly and efficiently</title>
	<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/</link>
	<description>Stay curious!</description>
	<pubDate>Sun, 20 Jul 2008 22:55:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: shelly</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14634</link>
		<author>shelly</author>
		<pubDate>Thu, 22 May 2008 12:07:49 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14634</guid>
		<description>Hi,
i need a script which can fix the length of a number with given number of digits (not adding decimal point). e.g. need to have a 2 digit number so method(5) should return 05.
i need this for month and days of a date. How to do this?</description>
		<content:encoded><![CDATA[<p>Hi,<br />
i need a script which can fix the length of a number with given number of digits (not adding decimal point). e.g. need to have a 2 digit number so method(5) should return 05.<br />
i need this for month and days of a date. How to do this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matias Moncho</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14419</link>
		<author>Matias Moncho</author>
		<pubDate>Tue, 08 Apr 2008 19:57:55 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14419</guid>
		<description>@Angel: Yes, somehow my clippboard didn't paste the whole code.
Here I let you a link to a file white the "round" method:
http://www.clanpesto.com.ar/round.js

Regards!</description>
		<content:encoded><![CDATA[<p>@Angel: Yes, somehow my clippboard didn&#8217;t paste the whole code.<br />
Here I let you a link to a file white the &#8220;round&#8221; method:<br />
<a href="http://www.clanpesto.com.ar/round.js" rel="nofollow">http://www.clanpesto.com.ar/round.js</a></p>
<p>Regards!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Angel</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14418</link>
		<author>Angel</author>
		<pubDate>Tue, 08 Apr 2008 18:57:57 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14418</guid>
		<description>Matias, something is missing in your code, before the brace ( { ) at the end of 
var strToRound = (toRound.toString().indexOf(".") = 5) 

?</description>
		<content:encoded><![CDATA[<p>Matias, something is missing in your code, before the brace ( { ) at the end of<br />
var strToRound = (toRound.toString().indexOf(&#8221;.&#8221;) = 5) </p>
<p>?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matias Moncho</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14414</link>
		<author>Matias Moncho</author>
		<pubDate>Mon, 07 Apr 2008 21:51:38 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14414</guid>
		<description>Hi, I found another bug on round function, even with Ron's patch.
As Ron said, if you enter: "11.95" with format "#.0" you get "12.0", that's ok.
But on negative numbers, ie: "-11.95" with format "#.0" you get "-10.0". That's wrong, because the nearest number to -11.95 is -12.

So, here is my patch for this function. I recoded it from scratch, And tested a bit.
[code]
Number.prototype.round = function(decimals) {
	if (decimals &#62; 0) {
		var toRound = this * (Math.pow(10, decimals));
		var strToRound = (toRound.toString().indexOf(".") = 5) {
			return ((parseInt(strToRound.substring(0, strToRound.indexOf(".")), 10)   (this &#62; 0 ? 1 : (-1))) / (Math.pow(10, decimals)));
		} else {
			return (parseInt(strToRound.substring(0, strToRound.indexOf(".")), 10) / (Math.pow(10, decimals)));
		}
	}
	return this;
}
[/code]

I hope that helps someone!.</description>
		<content:encoded><![CDATA[<p>Hi, I found another bug on round function, even with Ron&#8217;s patch.<br />
As Ron said, if you enter: &#8220;11.95&#8243; with format &#8220;#.0&#8243; you get &#8220;12.0&#8243;, that&#8217;s ok.<br />
But on negative numbers, ie: &#8220;-11.95&#8243; with format &#8220;#.0&#8243; you get &#8220;-10.0&#8243;. That&#8217;s wrong, because the nearest number to -11.95 is -12.</p>
<p>So, here is my patch for this function. I recoded it from scratch, And tested a bit.<br />
[code]<br />
Number.prototype.round = function(decimals) {<br />
	if (decimals &gt; 0) {<br />
		var toRound = this * (Math.pow(10, decimals));<br />
		var strToRound = (toRound.toString().indexOf(&#8221;.&#8221;) = 5) {<br />
			return ((parseInt(strToRound.substring(0, strToRound.indexOf(&#8221;.&#8221;)), 10)   (this &gt; 0 ? 1 : (-1))) / (Math.pow(10, decimals)));<br />
		} else {<br />
			return (parseInt(strToRound.substring(0, strToRound.indexOf(&#8221;.&#8221;)), 10) / (Math.pow(10, decimals)));<br />
		}<br />
	}<br />
	return this;<br />
}<br />
[/code]</p>
<p>I hope that helps someone!.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Angel</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14397</link>
		<author>Angel</author>
		<pubDate>Wed, 02 Apr 2008 23:45:03 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14397</guid>
		<description>Thanks for this excellent script, Xaprb. It's solved my problem in one go.
I am having a problem, though; I want fixed-width numbers so they are aligned in columns in a textarea, so I'm using "?????.####" as the format. However, if the numbers are negative, I get one more column since the minus sign is inserted after the spaces and before the number.
I need the minus sign to take the space of one of the "?", so the columns are aligned correctly irrespective of the sign.</description>
		<content:encoded><![CDATA[<p>Thanks for this excellent script, Xaprb. It&#8217;s solved my problem in one go.<br />
I am having a problem, though; I want fixed-width numbers so they are aligned in columns in a textarea, so I&#8217;m using &#8220;?????.####&#8221; as the format. However, if the numbers are negative, I get one more column since the minus sign is inserted after the spaces and before the number.<br />
I need the minus sign to take the space of one of the &#8220;?&#8221;, so the columns are aligned correctly irrespective of the sign.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bruce</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14210</link>
		<author>Bruce</author>
		<pubDate>Wed, 06 Feb 2008 00:35:56 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14210</guid>
		<description>Hey Xaprb, I tried the following in my code:

new Number(85.68).numberFormat('$#,###.##');

but it returned $86.00. I then tried the same number and format on your demo page, and it correctly displayed $85.68.

Is there a difference between the demo page code and what's in the download? My version of number-functions.js is dated 6/15/2007.</description>
		<content:encoded><![CDATA[<p>Hey Xaprb, I tried the following in my code:</p>
<p>new Number(85.68).numberFormat(&#8217;$#,###.##&#8217;);</p>
<p>but it returned $86.00. I then tried the same number and format on your demo page, and it correctly displayed $85.68.</p>
<p>Is there a difference between the demo page code and what&#8217;s in the download? My version of number-functions.js is dated 6/15/2007.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Livia</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14139</link>
		<author>Livia</author>
		<pubDate>Wed, 09 Jan 2008 09:15:56 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14139</guid>
		<description>Thanks very much! That was great, exacly what I needed</description>
		<content:encoded><![CDATA[<p>Thanks very much! That was great, exacly what I needed</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14121</link>
		<author>Jon</author>
		<pubDate>Thu, 03 Jan 2008 17:33:11 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-14121</guid>
		<description>Great script!</description>
		<content:encoded><![CDATA[<p>Great script!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-11738</link>
		<author>Xaprb</author>
		<pubDate>Wed, 20 Jun 2007 02:05:57 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-11738</guid>
		<description>A new version is released with a fix for the zero-padding of negative numbers.

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 "input", "format", "expected" is best.  For example,

-1, "#,#.00", "-1.00"

Is a great test case.  I can plug that directly into the unit tests, run it, and if it gives back "-01.00" it will fail the test.  This makes it much easier and more convenient for me to fix bugs.

&lt;a href="/blog/donate/" rel="nofollow"&gt;Sponsoring&lt;/a&gt; bug fixes won't hurt either ;-)</description>
		<content:encoded><![CDATA[<p>A new version is released with a fix for the zero-padding of 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,</p>
<p>-1, &#8220;#,#.00&#8243;, &#8220;-1.00&#8243;</p>
<p>Is a great test case.  I can plug that directly into the unit tests, 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/" rel="nofollow">Sponsoring</a> bug fixes won&#8217;t hurt either ;-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Janssen</title>
		<link>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-10319</link>
		<author>Andrew Janssen</author>
		<pubDate>Sat, 02 Jun 2007 00:21:16 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-10319</guid>
		<description>Thanks Xaprb. You rock.</description>
		<content:encoded><![CDATA[<p>Thanks Xaprb. You rock.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
