From: FAQ server on
-----------------------------------------------------------------------
FAQ Topic - How do I format a Number as a String with
exactly 2 decimal places?
-----------------------------------------------------------------------

When formatting money for example, to format 6.57634 to
6.58, 6.5 to 6.50, and 6 to 6.00?

Rounding of x.xx5 is uncertain, as such numbers are not
represented exactly. See also:

http://jibbering.com/faq/#binaryNumbers

The statement ` n = Math.round(n * 100)/100 ` converts ` n ` to a ` Number ` value
close to a multiple of ` 0.01 `. However, there are some problems.
Converting the number to a string ` (n + "") `, does not give
trailing zeroes. Rounding numbers that are very close to ` x.5 `, for example,
` Math.round(0.49999999999999992) ` results ` 1 `.

ECMAScript Ed. 3.0 introduced ` Number.prototype.toFixed `.
There are bugs in JScript's implementation with certain numbers,
for example ` 0.07 `.

Function ` numberToFixed ` returns accurate results that are
consistent across implementations where ` n > 0 `.

var numberToFixed =
(function() {
return toFixedString;

function toFixedString(n, digits) {
var unsigned = toUnsignedString(Math.abs(n), digits);
return (n < 0 ? "-" : "") + unsigned;
}

function toUnsignedString(n, digits) {
var t, s = Math.round(n * Math.pow(10, digits)) + "",
start, end;
if (/\D/.test(s)) {
return "" + n;
}
s = padLeft(s, 1 + digits, "0");
start = s.substring(0, t = (s.length - digits));
end = s.substring(t);
if(end) {
end = "." + end;
}
return start + end; // avoid "0."
}
/**
* @param {string} input: input value converted to string.
* @param {number} size: desired length of output.
* @param {string} ch: single character to prefix to s.
*/
function padLeft(input, size, ch) {
var s = input + "";
while(s.length < size) {
s = ch + s;
}
return s;
}
})();

// Test results
document.writeln([
"numberToFixed(9e-3, 12) => " + numberToFixed(9e-3, 12),
"numberToFixed(1.255, 2) => " + numberToFixed(1.255, 2),
"numberToFixed(1.355, 2) => " + numberToFixed(1.355, 2),
"numberToFixed(0.1255, 3) => " + numberToFixed(0.1255, 3),
"numberToFixed(0.07, 2) => " + numberToFixed(0.07, 2),
"numberToFixed(0.0000000006, 1) => " + numberToFixed(0.0000000006, 1),
"numberToFixed(0.0000000006, 0) => " + numberToFixed(0.0000000006, 0)
].join("\n"));

http://www.merlyn.demon.co.uk/js-round.htm

http://msdn.microsoft.com/en-us/library/sstyff0z%28VS.85%29.aspx


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

--

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

From: Dr J R Stockton on
In comp.lang.javascript message <4bb130fb$0$286$14726298(a)news.sunsite.dk
>, Mon, 29 Mar 2010 23:00:03, FAQ server <javascript(a)dotinternet.be>
posted:
>-----------------------------------------------------------------------
>FAQ Topic - How do I format a Number as a String with
>exactly 2 decimal places?
>-----------------------------------------------------------------------
>
>When formatting money for example, to format 6.57634 to
>6.58, 6.5 to 6.50, and 6 to 6.00?
>
>Rounding of x.xx5 is uncertain, as such numbers are not
^ most
>represented exactly.


>ECMAScript Ed. 3.0 introduced ` Number.prototype.toFixed `.
>There are bugs in JScript's implementation with certain numbers,
>for example ` 0.07 `.

"JScript" needs a "Microsoft", since otherwise it may be taken as a
typo.

There is no problem with 0.07.toFixed(2) (review Subject line) and
so the example should be 0.007.


>Function ` numberToFixed ` returns accurate results that are
>consistent across implementations where ` n > 0 `.
???????????????
That limitation should be inappropriate. If it is, give an example
which can be fixed.


It would be better not to use the identifier "n" in both sub-functions,
as it does not represent exactly the same thing. Use "m" in one case.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
From: Mike Duffy on
Garrett Smith <dhtmlkitchen(a)gmail.com> wrote in
news:hqgl62$ggc$1(a)news.eternal-september.org:

> Dr J R Stockton wrote:
>> In comp.lang.javascript message
>> <4bb130fb$0$286$14726298(a)news.sunsite.dk
>>> , Mon, 29 Mar 2010 23:00:03, FAQ server
>>> <javascript(a)dotinternet.be>
>> posted:
>>> -----------------------------------------------------------------
>>> ------ FAQ Topic - How do I format a Number as a String with
>>> exactly 2 decimal places?
>>> -----------------------------------------------------------------
>>> ------
>>>
>>> When formatting money for example, to format 6.57634 to
>>> 6.58, 6.5 to 6.50, and 6 to 6.00?

http://code.google.com/p/sprintf/
From: Jorge on
On Apr 19, 6:11 am, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
> Dr J R Stockton wrote:
>
> > "JScript" needs a "Microsoft", since otherwise it may be taken as a
> > typo.
>
> "Microsoft JScript" would be redundant.

In all truth, the Microsoft name must be there, in lights, next to the
words BUG and Internet Explorer and JScript, well spelled, with all
the letters.
--
Jorge.
From: Jorge on
On Apr 19, 1:29 pm, Jorge <jo...(a)jorgechamorro.com> wrote:
> On Apr 19, 6:11 am, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
>
> > Dr J R Stockton wrote:
>
> > > "JScript" needs a "Microsoft", since otherwise it may be taken as a
> > > typo.
>
> > "Microsoft JScript" would be redundant.
>
> In all truth, the Microsoft name must be there, in lights, next to the
> words BUG and Internet Explorer and JScript, well spelled, with all
> the letters.

And note that the best workaround for that bug is to switch to a
better browser.
--
Jorge.