Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2010-07-28)

5 views
Skip to first unread message

FAQ server

unread,
Jul 27, 2010, 7:00:03 PM7/27/10
to
-----------------------------------------------------------------------
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.7 to
6.50, and 6 to 6.00?

Rounding of x.xx5 is unreliable, as most numbers are not represented
exactly. See also:
Why does simple decimal arithmetic give strange results? [ref 1]

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`.

ECMA-262 3rd Edition introduced `Number.prototype.toFixed`.
There are bugs in JScript's implementation with certain numbers,
for example `0.07`.

var numberToFixed =
(function() {
return toFixedString;

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

function toUnsignedString(m, digits) {
var t, s = Math.round(m * Math.pow(10, digits)) + "",
start, end;
if (/\D/.test(s)) {
return "" + m;
}
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"));

<URL: http://www.merlyn.demon.co.uk/js-round.htm>
<URL: http://msdn.microsoft.com/en-us/library/sstyff0z%28VS.85%29.aspx>

References:
-----------

[1] http://jibbering.com/faq/#binaryNumbers


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.

Ry Nohryb

unread,
Jul 27, 2010, 7:24:25 PM7/27/10
to
On Jul 28, 1:00 am, "FAQ server" <javascr...@dotinternet.be> wrote:
> -----------------------------------------------------------------------
> FAQ Topic - How do I format a Number as a String with
> exactly 2 decimal places?
> -----------------------------------------------------------------------
> (...)

This is the problem. JavaScript coding style bent to suit IE's bugs.
NO, please, no. The proper way to do this is :

(number).toFixed(decimalPlaces) --> string

For example:

Math.PI.toFixed(3)
"3.142"

or

(6).toFixed(2)
--> "6.00"

That's what ought to come FIRST in this FAQ entry, and it should be
the recommended way of doing it.

Then, -*only*after*having*said*that*in*the*first*place*-, you can also
say that IEs have got (yet another) bug that affects
Number.prototype.toFixed(), sooo, in IEs you could do instead:

(blah, blah, blah)
--
Jorge.

kangax

unread,
Jul 28, 2010, 12:16:43 PM7/28/10
to

Does `numberToFixed` from the FAQ entry solve anything that fully
compliant `Number.prototype.toFixed` doesn't? From what I can see, they
should be identical.

If they are, then why not employ a feature test and skip workaround once
implementation is determined to be compliant? At least for performance
reasons.

--
kangax

David Mark

unread,
Jul 28, 2010, 11:16:29 PM7/28/10
to

Haven't looked at it. Will check when I have a chance.

>
> If they are, then why not employ a feature test and skip workaround once
> implementation is determined to be compliant? At least for performance
> reasons.
>

I've got one here somewhere. In my branch of Dojo I think (replaced a
UA sniff for IE). :)

Dr J R Stockton

unread,
Jul 29, 2010, 3:57:52 PM7/29/10
to
In comp.lang.javascript message <k-idncXl5612ys3RnZ2dnUVZ_rqdnZ2d@gigane
ws.com>, Wed, 28 Jul 2010 12:16:43, kangax <kan...@gmail.com> posted:

>Does `numberToFixed` from the FAQ entry solve anything that fully
>compliant `Number.prototype.toFixed` doesn't? From what I can see, they
>should be identical.

In FF 3.0.19 and IE8, (-0).toFixed(2) wrongly gives '0.00'. The code in
the FAQ could be corrected to fix that too.

>If they are, then why not employ a feature test and skip workaround
>once implementation is determined to be compliant? At least for
>performance reasons.

Using a feature test means that the code must be tested in at least two
browsers. It means that more code must be downloaded.

And, considering the likely usage as being for display (or for writing
to file with WSH JScript), does speed matter?

Doing StrS(Math.PI, 3, 5) on this machine takes under 24 us in FireFox,
and StrS is similar to the FAQ code; doing Math.PI.toFixed(5) takes
under 4 us. Will that 20 us difference matter significantly often?

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

Dr J R Stockton

unread,
Jul 29, 2010, 4:03:24 PM7/29/10
to
In comp.lang.javascript message <4c4f64f9$0$279$1472...@news.sunsite.dk
>, Tue, 27 Jul 2010 23:00:03, FAQ server <javas...@dotinternet.be>
posted:


>FAQ Topic - How do I format a Number as a String with
>exactly 2 decimal places?

>ECMA-262 3rd Edition introduced `Number.prototype.toFixed`.
>There are bugs in JScript's implementation with certain numbers,
>for example `0.07`.

No, there is no bug with toFixed formatting 0.07 as per Subject.

0.07.toFixed(2) -> '0.07'

You have been told, more than once IIRC, and have agreed at least once,
that the example should be 0.007 or similar.

NOW is the time to make the correction.

There is, in addition, a bug in Opera's toPrecision :

0.1230.toPrecision(4) -> 0.123 // should be 0.1230


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

That will not give the correct sign for n == -0 .

In the FAQ, sign should be given by a function such as

function Sign(X) { return X>0 ? "+" : X<0 ? "-" : " " }

Readers can more easily simplify that where needed than they can add to
the simplistic form in the FAQ.

There should also be a mention of testing the sign of (X+1/X).

--
(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)

David Mark

unread,
Jul 29, 2010, 6:53:10 PM7/29/10
to
On Jul 29, 3:57 pm, Dr J R Stockton <reply1...@merlyn.demon.co.uk>
wrote:

> In comp.lang.javascript message <k-idncXl5612ys3RnZ2dnUVZ_rqdnZ2d@gigane
> ws.com>, Wed, 28 Jul 2010 12:16:43, kangax <kan...@gmail.com> posted:
>
> >Does `numberToFixed` from the FAQ entry solve anything that fully
> >compliant `Number.prototype.toFixed` doesn't? From what I can see, they
> >should be identical.
>
> In FF 3.0.19 and IE8, (-0).toFixed(2) wrongly gives '0.00'.  The code in
> the FAQ could be corrected to fix that too.
>
> >If they are, then why not employ a feature test and skip workaround
> >once implementation is determined to be compliant? At least for
> >performance reasons.
>
> Using a feature test means that the code must be tested in at least two
> browsers.  It means that more code must be downloaded.
>
> And, considering the likely usage as being for display (or for writing
> to file with WSH JScript), does speed matter?
>
> Doing StrS(Math.PI, 3, 5) on this machine takes under 24 us in FireFox,
> and StrS is similar to the FAQ code; doing Math.PI.toFixed(5) takes
> under 4 us.  Will that 20 us difference matter significantly often?

As always, it depends on the context. That's one of the problems
inherent to writing GP code.

dhtml

unread,
Jul 29, 2010, 7:45:57 PM7/29/10
to

Function `numberToFixed` is intended as a fallback for buggy JScript.
The function itself is still somewhat undesirable because it relies on
IEEE754 rules and so sometimes you'll see a result that is odd.

javascript:alert(1.255.toFixed(2));

May result 1.25 or 1.26, depending on the implementation.

> If they are, then why not employ a feature test and skip workaround once
> implementation is determined to be compliant? At least for performance
> reasons.
>

Sure, use `Number.prototype.toFixed` where the known errors aren't
apparent. I remember struggling with trying to nail down identifying
what is buggy and what is acceptable and I ended up coming to writing
a function that will work consistently across browsers without having
to identify various differences.

Posting now via GG, which is not quite as "Comcastic" as my ISP, which
is almost on the same level as Dell computer manufacturer, which
claims that Win 7 cannot run on the computer I own (only the wireless
network drivers fail).

"Don't own a computer on this list? Click here to shop for a new
Dell!" http://dell.to/df9KMn

Most of the pages for Dell work in Firefox, but many are IE 5.5+
only.

http://support.dell.com/support/topics/global.aspx/support/my_systems_info/en/FindTagSysProfLicense?c=us&cs=19&l=en&parent=formSupportRequestEnterTag&s=dhs&~mode=popup

--
Garrett

dhtml

unread,
Jul 29, 2010, 7:53:11 PM7/29/10
to
On Jul 29, 1:03 pm, Dr J R Stockton <reply1...@merlyn.demon.co.uk>
wrote:
> In comp.lang.javascript message <4c4f64f9$0$279$14726...@news.sunsite.dk>, Tue, 27 Jul 2010 23:00:03, FAQ server <javascr...@dotinternet.be>

>
> posted:
>
> >FAQ Topic - How do I format a Number as a String with
> >exactly 2 decimal places?
> >ECMA-262 3rd Edition introduced `Number.prototype.toFixed`.
> >There are bugs in JScript's implementation with certain numbers,
> >for example `0.07`.
>
> No, there is no bug with toFixed formatting 0.07 as per Subject.
>
>         0.07.toFixed(2)         ->      '0.07'
>
> You have been told, more than once IIRC, and have agreed at least once,
> that the example should be 0.007 or similar.
>
> NOW is the time to make the correction.
>

I believe that I had changed it from 0.007 to 0.07 per your request,
though I don't have the thread on hand. I see that introduced a
mistake and so I'll change it back.
--
Garrett

Dr J R Stockton

unread,
Jul 31, 2010, 4:21:04 PM7/31/10
to
In comp.lang.javascript message <a44d7175-3081-4ecf-b42a-1196395154ba@q1
6g2000prf.googlegroups.com>, Thu, 29 Jul 2010 16:53:11, dhtml
<dhtmlk...@gmail.com> posted:

>On Jul 29, 1:03 pm, Dr J R Stockton <reply1...@merlyn.demon.co.uk>
>wrote:
>> In comp.lang.javascript message <4c4f64f9$0$279$14726...@news.sunsite
>>.dk>, Tue, 27 Jul 2010 23:00:03, FAQ server
>><javascr...@dotinternet.be>
>>
>> posted:
>>
>> >FAQ Topic - How do I format a Number as a String with
>> >exactly 2 decimal places?
>> >ECMA-262 3rd Edition introduced `Number.prototype.toFixed`.
>> >There are bugs in JScript's implementation with certain numbers,
>> >for example `0.07`.
>>
>> No, there is no bug with toFixed formatting 0.07 as per Subject.
>>
>>         0.07.toFixed(2)         ->      '0.07'
>>
>> You have been told, more than once IIRC, and have agreed at least once,
>> that the example should be 0.007 or similar.
>>
>> NOW is the time to make the correction.
>>
>
>I believe that I had changed it from 0.007 to 0.07 per your request,

I think not. It was 0.07 in versions 8.1 (Cornford) and 9.91 (Webb).

>though I don't have the thread on hand. I see that introduced a
>mistake and so I'll change it back.

Good.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

Dr J R Stockton

unread,
Jul 31, 2010, 4:17:02 PM7/31/10
to
In comp.lang.javascript message <9b483dcc-3075-40f4-9e5a-eaa9404ad31d@o7
g2000prg.googlegroups.com>, Thu, 29 Jul 2010 16:45:57, dhtml
<dhtmlk...@gmail.com> posted:

>
>Function `numberToFixed` is intended as a fallback for buggy JScript.
>The function itself is still somewhat undesirable because it relies on
>IEEE754 rules and so sometimes you'll see a result that is odd.

It is specified in ECMA. If a browser maker wishes to implement it
using a FPU, he must check that the FPU is ECMA-compliant, whatever IEEE
754 says.

ISTM likely that 754 supports both compliant rounding and some other
form, such as Bankers'; and possible that MS chose wrongly.
<URL:http://www.merlyn.demon.co.uk/js-rndg1.htm#toF> demonstrates that
MS are a load of Bankers.

>javascript:alert(1.255.toFixed(2));
>
>May result 1.25 or 1.26, depending on the implementation.
>

AFAICS, ECMA 262 5 15.7.4.5 8 requires rounding up, if the (IEEE Double)
Number is equally near to two adjacent strings. But Web page
<URL:http://www.merlyn.demon.co.uk/js-exact.htm#DW4> shows that the
literal 1.255 will be stored as 3ff4147ae147ae14, the value of which is
+1.25499999999999989341858963598497211933135986328125 - therefore, the
correct result is 1.25.

Firefox 3.0.19, Opera 10.10, Safari 5.0, Chrome 5.0 give 1.25;
MSIE 8 gives 1.26;
StrU("1.255", 0, 2), which the FAQ method resembles, gives 1.25.

YAMB (Yet Another Microsoft Bug). Now in <js-datex.htm>.

--

Garrett Smith

unread,
Aug 6, 2010, 5:26:23 PM8/6/10
to

Reinvestigating this, I see that .07.toFixed(1) rounds to 0 in IE 6-8
and 1 in IE9.

Doesn't that example show that the text in the FAQ is therefore correct?
--
Garrett

Dr J R Stockton

unread,
Aug 7, 2010, 3:57:59 PM8/7/10
to
In comp.lang.javascript message <i3huli$dkv$1...@news.eternal-
september.org>, Fri, 6 Aug 2010 14:26:23, Garrett Smith
<dhtmlk...@gmail.com> posted:

>> I believe that I had changed it from 0.007 to 0.07 per your request,
>> though I don't have the thread on hand. I see that introduced a
>> mistake and so I'll change it back.
>
>Reinvestigating this, I see that .07.toFixed(1) rounds to 0 in IE 6-8
>and 1 in IE9.
>
>Doesn't that example show that the text in the FAQ is therefore
>correct?


No, because the Subject line says "exactly 2 decimal places". The
expression 0.07.toFixed(2) works correctly in IE. Just use 0.007.

It is bad practice to write a number with a decimal point at one end or
the other, except where necessary in tabular material; a decimal point
is an easily-missed character.

Additionally, 0.1230.toPrecision(4) which should give '0.1230' gives
'0.123' in at least some Opera 9 & 10 (0.1234 gives '0.1234'). That is
numerically unimportant but it can do visually horrible things if the
length of the string affects a table column's width and the number is
being rapidly updated.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 7.


Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.

Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.

Garrett Smith

unread,
Aug 8, 2010, 12:46:40 AM8/8/10
to
On 2010-08-07 12:57 PM, Dr J R Stockton wrote:
> In comp.lang.javascript message<i3huli$dkv$1...@news.eternal-
> september.org>, Fri, 6 Aug 2010 14:26:23, Garrett Smith
> <dhtmlk...@gmail.com> posted:
>
>>> I believe that I had changed it from 0.007 to 0.07 per your request,
>>> though I don't have the thread on hand. I see that introduced a
>>> mistake and so I'll change it back.
>>
>> Reinvestigating this, I see that .07.toFixed(1) rounds to 0 in IE 6-8
>> and 1 in IE9.
>>
>> Doesn't that example show that the text in the FAQ is therefore
>> correct?
>
>
> No, because the Subject line says "exactly 2 decimal places". The
> expression 0.07.toFixed(2) works correctly in IE. Just use 0.007.
>

The entry has:

| There are bugs in JScript's implementation with certain numbers, for

| example 0.07.

More explicit:
| There are bugs in versions of Microsoft's implementation (JScript
| <= 5.8) with certain numbers, for example 0.007.toFixed(2) results
| "0.00" instead of "0.01".

I see also in the entry, the first line indicates that `6.7` can be
formatted to `6.50`. Doing that requires more than this function
provides. It seems `6.7` to `"6.70"` makes more sense, no?

So instead of:


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

Use:
| When formatting money for example, how to format 6.57634 to "6.58",
| 6.7 to "6.70", and 6 to 6.00?

The entry does not address rounding seen with numbers, as
numberToFixed(1.255, 2) === "1.25".

MSIE and some versions of Webkit will perform rounding with
`Number.prototype.toFixed`. This behavior seems more desirable for
formatting money because rounding 1.255 to "1.25" and 1.355 to "1.36" is
inconsistent. The current FAQ code does not round and the standard does
not specify rounding, either.

[...]
--
Garrett

Dr J R Stockton

unread,
Aug 9, 2010, 1:14:39 PM8/9/10
to
In comp.lang.javascript message <i3lcr3$vjp$1...@news.eternal-
september.org>, Sat, 7 Aug 2010 21:46:40, Garrett Smith
<dhtmlk...@gmail.com> posted:

>On 2010-08-07 12:57 PM, Dr J R Stockton wrote:
>> In comp.lang.javascript message<i3huli$dkv$1...@news.eternal-
>> september.org>, Fri, 6 Aug 2010 14:26:23, Garrett Smith
>> <dhtmlk...@gmail.com> posted:
>>
>>>> I believe that I had changed it from 0.007 to 0.07 per your request,
>>>> though I don't have the thread on hand. I see that introduced a
>>>> mistake and so I'll change it back.
>>>
>>> Reinvestigating this, I see that .07.toFixed(1) rounds to 0 in IE 6-8
>>> and 1 in IE9.
>>>
>>> Doesn't that example show that the text in the FAQ is therefore
>>> correct?
>>
>>
>> No, because the Subject line says "exactly 2 decimal places". The
>> expression 0.07.toFixed(2) works correctly in IE. Just use 0.007.
>>
>
>The entry has:
>
>| There are bugs in JScript's implementation with certain numbers, for
>| example 0.07.
>
>More explicit:
>| There are bugs in versions of Microsoft's implementation (JScript
>| <= 5.8) with certain numbers, for example 0.007.toFixed(2) results
>| "0.00" instead of "0.01".

Do you have Microsoft Word? I think that you would find it useful as a
grammar/style checker as well as for spelling and other typos. Change
"results" to "gives".

>I see also in the entry, the first line indicates that `6.7` can be
>formatted to `6.50`. Doing that requires more than this function
>provides. It seems `6.7` to `"6.70"` makes more sense, no?

It was "6.5 to 6.50" in FAQ 9.91 - 2008-01-19, which I believe was Randy
Webb's last version.


>So instead of:
>| When formatting money for example, to format 6.57634 to 6.58, 6.7 to
>| 6.50, and 6 to 6.00?
>
>Use:
>| When formatting money for example, how to format 6.57634 to "6.58",
>| 6.7 to "6.70", and 6 to 6.00?

Inconsistent use of "".

>The entry does not address rounding seen with numbers, as
>numberToFixed(1.255, 2) === "1.25".


>MSIE and some versions of Webkit will perform rounding with
>`Number.prototype.toFixed`. This behavior seems more desirable for
>formatting money because rounding 1.255 to "1.25" and 1.355 to "1.36"
>is inconsistent. The current FAQ code does not round and the standard
>does not specify rounding, either.

That appears to be complete nonsense. ECMA 3 & 5 do specify rounding.

There should be NO encouragement, or apparent encouragement, for doing
money arithmetic with "floats with two decimal places". For accounting,
work in pence, convert to String, and insert the separator with a string
operation. For budgeting, where accuracy is not needed, one can work in
float $k, $M, $G, $T, or local equivalent.

I don't think that you understand floating point and English well enough
to write usefully on the subject.

For a start, JavaScript CANNOT round the Number value 1.255 because an
IEEE Double cannot hold 1.255.

+"1.255" -> +1.25499999999999989341858963598497211933135986328125
+"1.355" -> +1.354999999999999982236431605997495353221893310546875
and, in FF 3.0.19 and Chrome 5.0, .toFixed(2) gives 1.25, 1.35 as is
proper; but MS IE 8 gives 1.26, 1.36, doing Banker's Rounding.

Read the relevant pages on my site.

--

(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.

Garrett Smith

unread,
Aug 9, 2010, 10:18:17 PM8/9/10
to
On 2010-08-09 10:14 AM, Dr J R Stockton wrote:
> In comp.lang.javascript message<i3lcr3$vjp$1...@news.eternal-

> september.org>, Sat, 7 Aug 2010 21:46:40, Garrett Smith
> <dhtmlk...@gmail.com> posted:
>
>> On 2010-08-07 12:57 PM, Dr J R Stockton wrote:
>>> In comp.lang.javascript message<i3huli$dkv$1...@news.eternal-
>>> september.org>, Fri, 6 Aug 2010 14:26:23, Garrett Smith
>>> <dhtmlk...@gmail.com> posted:
>>>
>>>>> I believe that I had changed it from 0.007 to 0.07 per your request,
>>>>> though I don't have the thread on hand. I see that introduced a
>>>>> mistake and so I'll change it back.
>>>>
>>>> Reinvestigating this, I see that .07.toFixed(1) rounds to 0 in IE 6-8
>>>> and 1 in IE9.
>>>>
>>>> Doesn't that example show that the text in the FAQ is therefore
>>>> correct?
>>>
>>>
>>> No, because the Subject line says "exactly 2 decimal places". The
>>> expression 0.07.toFixed(2) works correctly in IE. Just use 0.007.
>>>
>>
>> The entry has:
>>
>> | There are bugs in JScript's implementation with certain numbers, for
>> | example 0.07.
>>
>> More explicit:
>> | There are bugs in versions of Microsoft's implementation (JScript
>> |<= 5.8) with certain numbers, for example 0.007.toFixed(2) results
>> | "0.00" instead of "0.01".
>
> Do you have Microsoft Word?

No. The version that I purchased with this machine, purchased from Dave
Kaercher, through eBay, was unlicensed or pirated, along with everything
else that came on this machine.

I think that you would find it useful as a
> grammar/style checker as well as for spelling and other typos. Change
> "results" to "gives".
>

Why?

>> I see also in the entry, the first line indicates that `6.7` can be
>> formatted to `6.50`. Doing that requires more than this function
>> provides. It seems `6.7` to `"6.70"` makes more sense, no?
>
> It was "6.5 to 6.50" in FAQ 9.91 - 2008-01-19, which I believe was Randy
> Webb's last version.
>
>

Does it matter?

[...]


>
> I don't think that you understand floating point and English well enough
> to write usefully on the subject.
>

Well numbers are certainly more your thing. Do you have a proposal?

> For a start, JavaScript CANNOT round the Number value 1.255 because an
> IEEE Double cannot hold 1.255.
>
> +"1.255" -> +1.25499999999999989341858963598497211933135986328125
> +"1.355" -> +1.354999999999999982236431605997495353221893310546875
> and, in FF 3.0.19 and Chrome 5.0, .toFixed(2) gives 1.25, 1.35 as is
> proper; but MS IE 8 gives 1.26, 1.36, doing Banker's Rounding.
>
> Read the relevant pages on my site.
>

Do you have a javascript converter to get the IEEE754 representation
from a source number?

Most of what is on your site features a style of coding using single
letter identifiers and abbreviations. I find this difficult to
understand. Then again, I was not sure exactly what I should be looking
for in pages linked from the FAQ entry.
--
Garrett

Dr J R Stockton

unread,
Aug 11, 2010, 6:05:08 PM8/11/10
to
In comp.lang.javascript message <i3qcta$uc3$1...@news.eternal-
september.org>, Mon, 9 Aug 2010 19:18:17, Garrett Smith
<dhtmlk...@gmail.com> posted:

No need to bloat the entry; just change the 0.7 to 0.007. You should
also change it in the // Test results part that you added.

>> Do you have Microsoft Word?
>
>No. The version that I purchased with this machine, purchased from Dave
>Kaercher, through eBay, was unlicensed or pirated, along with
>everything else that came on this machine.

Open office may do as well.

>I think that you would find it useful as a
>> grammar/style checker as well as for spelling and other typos. Change
>> "results" to "gives".
>
>Why?

Because it is better English; and because "results in" would be longer.
Remember, you are not paid by the word-count. And change
`Math.round(0.49999999999999992)` results `1`.
correspondingly.

>>> I see also in the entry, the first line indicates that `6.7` can be
>>> formatted to `6.50`. Doing that requires more than this function
>>> provides. It seems `6.7` to `"6.70"` makes more sense, no?
>>
>> It was "6.5 to 6.50" in FAQ 9.91 - 2008-01-19, which I believe was Randy
>> Webb's last version.
>>
>>
>Does it matter?

It strongly suggests that you created the error.

>[...]
>>
>> I don't think that you understand floating point and English well enough
>> to write usefully on the subject.
>
>Well numbers are certainly more your thing. Do you have a proposal?

That you stop writing on the subject.

>> For a start, JavaScript CANNOT round the Number value 1.255 because an
>> IEEE Double cannot hold 1.255.
>>
>> +"1.255" -> +1.25499999999999989341858963598497211933135986328125
>> +"1.355" -> +1.354999999999999982236431605997495353221893310546875
>> and, in FF 3.0.19 and Chrome 5.0, .toFixed(2) gives 1.25, 1.35 as is
>> proper; but MS IE 8 gives 1.26, 1.36, doing Banker's Rounding.
>>
>> Read the relevant pages on my site.
>>
>Do you have a javascript converter to get the IEEE754 representation
>from a source number?

Read my site; or read the archives of this newsgroup.

>Most of what is on your site features a style of coding using single
>letter identifiers and abbreviations. I find this difficult to
>understand.

Were you never taught algebra at school?

> Then again, I was not sure exactly what I should be looking for in
>pages linked from the FAQ entry.

For that, you should blame the FAQ maintainer.

If you want to know about doing something exactly in JavaScript, would
it not be a good idea to try the JavaScript index page js-index.htm and
observe the appearance of the word "Exact", linking to js-exact.htm?

Garrett Smith

unread,
Aug 12, 2010, 1:17:21 AM8/12/10
to
On 2010-08-11 03:05 PM, Dr J R Stockton wrote:
> In comp.lang.javascript message<i3qcta$uc3$1...@news.eternal-

If JScript is mentioned, the version should be mentioned as well.
Otherwise, it would be better to say "some implementations".

| There are bugs in some implementations. JScript <= 5.8, for example


| 0.007.toFixed(2) results "0.00" instead of "0.01".

Better?

[...]

>> [...]
>>>
>>> I don't think that you understand floating point and English well enough
>>> to write usefully on the subject.
>>
>> Well numbers are certainly more your thing. Do you have a proposal?
>
> That you stop writing on the subject.
>

I am not putting that in the FAQ.

[...]


>>>
>> Do you have a javascript converter to get the IEEE754 representation
>>from a source number?
>
> Read my site; or read the archives of this newsgroup.
>

I'll take that as a no.

>> Most of what is on your site features a style of coding using single
>> letter identifiers and abbreviations. I find this difficult to
>> understand.
>
> Were you never taught algebra at school?
>

I'm giving you feedback on your style of coding. Take it of leave it.

>> Then again, I was not sure exactly what I should be looking for in
>> pages linked from the FAQ entry.
>
> For that, you should blame the FAQ maintainer.
>

OK, so it seems that page doesn't have strongly relevant material. Fine,
I'll remove the link then.

> If you want to know about doing something exactly in JavaScript, would
> it not be a good idea to try the JavaScript index page js-index.htm and
> observe the appearance of the word "Exact", linking to js-exact.htm?
>

Why don't you post a link?
--
Garrett

Garrett Smith

unread,
Aug 12, 2010, 1:19:36 AM8/12/10
to
On 2010-08-11 03:05 PM, Dr J R Stockton wrote:
> In comp.lang.javascript message<i3qcta$uc3$1...@news.eternal-

> september.org>, Mon, 9 Aug 2010 19:18:17, Garrett Smith
> <dhtmlk...@gmail.com> posted:
>
>> On 2010-08-09 10:14 AM, Dr J R Stockton wrote:
>>> In comp.lang.javascript message<i3lcr3$vjp$1...@news.eternal-
>>> september.org>, Sat, 7 Aug 2010 21:46:40, Garrett Smith
>>> <dhtmlk...@gmail.com> posted:
>>>
>>>> On 2010-08-07 12:57 PM, Dr J R Stockton wrote:
>>>>> In comp.lang.javascript message<i3huli$dkv$1...@news.eternal-
>>>>> september.org>, Fri, 6 Aug 2010 14:26:23, Garrett Smith
>>>>> <dhtmlk...@gmail.com> posted:
>>>>>

[...]

> Open office may do as well.
>

I'll check it out.

>> I think that you would find it useful as a
>>> grammar/style checker as well as for spelling and other typos. Change
>>> "results" to "gives".
>>
>> Why?
>
> Because it is better English; and because "results in" would be longer.
> Remember, you are not paid by the word-count. And change
> `Math.round(0.49999999999999992)` results `1`.
> correspondingly.
>

How is it better English? What am I missing here?
--
Garrett

Garrett Smith

unread,
Aug 12, 2010, 1:25:54 AM8/12/10
to

Missing a comma. Could also reverse the order of parts:

...For example, JScript <= 5.8 0.007.toFixed(2) results "0.00" instead
of "0.01".
--
Garrett

Dr J R Stockton

unread,
Aug 13, 2010, 6:08:49 PM8/13/10
to
In comp.lang.javascript message <i4004v$djc$1...@news.eternal-
september.org>, Wed, 11 Aug 2010 22:17:21, Garrett Smith
<dhtmlk...@gmail.com> posted:

>On 2010-08-11 03:05 PM, Dr J R Stockton wrote:


>If JScript is mentioned, the version should be mentioned as well.
>Otherwise, it would be better to say "some implementations".
>
>| There are bugs in some implementations. JScript <= 5.8, for example
>| 0.007.toFixed(2) results "0.00" instead of "0.01".
>

That requires that you know, for certain, that versions above 5.8 are
correct, since it may be understood as implying such. And you still
need to correct the grammar. Try reading Trollope.


>>> [...]
>>>>
>>>> I don't think that you understand floating point and English well enough
>>>> to write usefully on the subject.
>>>
>>> Well numbers are certainly more your thing. Do you have a proposal?
>>
>> That you stop writing on the subject.
>
>I am not putting that in the FAQ.

Rather a silly response.

>>> Do you have a javascript converter to get the IEEE754 representation
>>>from a source number?
>>
>> Read my site; or read the archives of this newsgroup.
>
>I'll take that as a no.

Rather a silly response.


>>> Most of what is on your site features a style of coding using single
>>> letter identifiers and abbreviations. I find this difficult to
>>> understand.
>>
>> Were you never taught algebra at school?
>
>I'm giving you feedback on your style of coding. Take it of leave it.

I rarely take your advice.

You can, of course, copy the code and edit it into your preferred style
- but you cannot legally publish such a derivative version.


>>> Then again, I was not sure exactly what I should be looking for in
>>> pages linked from the FAQ entry.
>>
>> For that, you should blame the FAQ maintainer.
>
>OK, so it seems that page doesn't have strongly relevant material.
>Fine, I'll remove the link then.

You're being silly again. That page deals with rounding, which is
necessarily an inexact operation, even though the requirements may be
well defined. For exact operations, start elsewhere - such as the index
pages.

>> If you want to know about doing something exactly in JavaScript, would
>> it not be a good idea to try the JavaScript index page js-index.htm and
>> observe the appearance of the word "Exact", linking to js-exact.htm?
>>
>Why don't you post a link?

Because you had all the information that was necessary, in that article.
And you should not put that link into the FAQ section under discussion.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3 6.20 ; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Dr J R Stockton

unread,
Aug 13, 2010, 6:12:54 PM8/13/10
to
In comp.lang.javascript message <i40094$dkb$1...@news.eternal-
september.org>, Wed, 11 Aug 2010 22:19:36, Garrett Smith
<dhtmlk...@gmail.com> posted:

>On 2010-08-11 03:05 PM, Dr J R Stockton wrote:
>> In comp.lang.javascript message<i3qcta$uc3$1...@news.eternal-
>> september.org>, Mon, 9 Aug 2010 19:18:17, Garrett Smith
>> <dhtmlk...@gmail.com> posted:
>>
>>> On 2010-08-09 10:14 AM, Dr J R Stockton wrote:

>>> I think that you would find it useful as a
>>>> grammar/style checker as well as for spelling and other typos. Change
>>>> "results" to "gives".
>>>
>>> Why?
>>
>> Because it is better English; and because "results in" would be longer.

>How is it better English? What am I missing here?

Education.

Garrett Smith

unread,
Aug 13, 2010, 7:43:33 PM8/13/10
to
On 2010-08-13 03:08 PM, Dr J R Stockton wrote:
> In comp.lang.javascript message<i4004v$djc$1...@news.eternal-
> september.org>, Wed, 11 Aug 2010 22:17:21, Garrett Smith
> <dhtmlk...@gmail.com> posted:
>
>> On 2010-08-11 03:05 PM, Dr J R Stockton wrote:
>
>
>> If JScript is mentioned, the version should be mentioned as well.
>> Otherwise, it would be better to say "some implementations".
>>
>> | There are bugs in some implementations. JScript<= 5.8, for example
>> | 0.007.toFixed(2) results "0.00" instead of "0.01".
>>
>
> That requires that you know, for certain, that versions above 5.8 are
> correct, since it may be understood as implying such. And you still
> need to correct the grammar. Try reading Trollope.
>
>

It has been observed that in the script engine for IE9 beta, the bugs
regarding 0.007 and 0.07 are no longer present.

Statement that JSCript 5.8 and below are buggy does not imply that
versions above are completely error-free.

[...]


>
>>> If you want to know about doing something exactly in JavaScript, would
>>> it not be a good idea to try the JavaScript index page js-index.htm and
>>> observe the appearance of the word "Exact", linking to js-exact.htm?
>>>
>> Why don't you post a link?
>
> Because you had all the information that was necessary, in that article.
> And you should not put that link into the FAQ section under discussion.
>

Let me get this straight: You did post a link because I should not put
that link into the FAQ section under discussion? Were you
second-guessing my actions? What's stopping me from adding a link if I
found the page ulteriorly?

Bonus question: Is such thinking is acquired by what you call 'education'?

Oh - Eclipse is up -- gotta get back to it...
--
Garrett

Garrett Smith

unread,
Aug 13, 2010, 7:48:01 PM8/13/10
to
On 2010-08-13 04:43 PM, Garrett Smith wrote:
> On 2010-08-13 03:08 PM, Dr J R Stockton wrote:
>> In comp.lang.javascript message<i4004v$djc$1...@news.eternal-
>> september.org>, Wed, 11 Aug 2010 22:17:21, Garrett Smith
>> <dhtmlk...@gmail.com> posted:
>>
>>> On 2010-08-11 03:05 PM, Dr J R Stockton wrote:

[...]


> Let me get this straight: You did post a link because I should not put

s/did/didn't
--
Garrett

Dr J R Stockton

unread,
Aug 15, 2010, 4:30:49 PM8/15/10
to
In comp.lang.javascript message <i44lb1$mls$1...@news.eternal-
september.org>, Fri, 13 Aug 2010 16:43:33, Garrett Smith
<dhtmlk...@gmail.com> posted:

>On 2010-08-13 03:08 PM, Dr J R Stockton wrote:
>> In comp.lang.javascript message<i4004v$djc$1...@news.eternal-
>> september.org>, Wed, 11 Aug 2010 22:17:21, Garrett Smith
>> <dhtmlk...@gmail.com> posted:
>>
>>> On 2010-08-11 03:05 PM, Dr J R Stockton wrote:
>>
>>> If JScript is mentioned, the version should be mentioned as well.
>>> Otherwise, it would be better to say "some implementations".
>>>
>>> | There are bugs in some implementations. JScript<= 5.8, for example
>>> | 0.007.toFixed(2) results "0.00" instead of "0.01".
>>
>> That requires that you know, for certain, that versions above 5.8 are
>> correct, since it may be understood as implying such. And you still
>> need to correct the grammar. Try reading Trollope.
>>
>
>It has been observed that in the script engine for IE9 beta, the bugs
>regarding 0.007 and 0.07 are no longer present.

How about the errors in converting fairly large power-of-ten integer
strings to Number?


>Statement that JSCript 5.8 and below are buggy does not imply that
>versions above are completely error-free.

That depends on how well they are written. The form "<=5.8" can easily
be read as implying that >5.8 is known to be OK. That may be so; but
testing IE9 beta's engine does not prove that unless it is known to use
the version immediately following 5.8.

Using "up to at least JScript 5.8" would be perfectly safe if nothing
was known about anything later than current IE8.

>>>> If you want to know about doing something exactly in JavaScript, would
>>>> it not be a good idea to try the JavaScript index page js-index.htm and
>>>> observe the appearance of the word "Exact", linking to js-exact.htm?
>>>>
>>> Why don't you post a link?
>>
>> Because you had all the information that was necessary, in that article.
>> And you should not put that link into the FAQ section under discussion.
>>
>Let me get this straight: You did post a link because I should not put
>that link into the FAQ section under discussion? Were you second-
>guessing my actions? What's stopping me from adding a link if I found
>the page ulteriorly?

Only intelligence would do that. You were asking about code which it
would not be appropriate to cite under the present Subject. I mentioned
the code because you asked if I had such. And I did not say that I had
posted a link; rather than that I had posted the necessary information.

To your follow-up article : no.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE8 FF3 Op10 Sf4 Cr4
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Thomas 'PointedEars' Lahn

unread,
Oct 10, 2010, 8:23:19 AM10/10/10
to
Garrett Smith wrote:

> Dr J R Stockton wrote:

>> Garrett Smith posted:


>>> Dr J R Stockton wrote:

>>>> Garrett Smith posted:


>>>>> More explicit:
>>>>> | There are bugs in versions of Microsoft's implementation (JScript
>>>>> |<= 5.8) with certain numbers, for example 0.007.toFixed(2) results
>>>>> | "0.00" instead of "0.01".

>>>> I think that you would find it useful as a grammar/style checker as
>>>> well as for spelling and other typos. Change "results" to "gives".
>>> Why?
>> Because it is better English; and because "results in" would be longer.
>> Remember, you are not paid by the word-count. And change
>> `Math.round(0.49999999999999992)` results `1`.
>> correspondingly.
>
> How is it better English? What am I missing here?

AFAIK "results" requires a preposition, "in", so "results in (1)" would not
be better English, it would be grammatical correct English in the first
place.

However, I do not think that "gives" (which does not require a preposition)
is better English than "results in". ISTM that the former is a synonym for
the latter in informal language (compare "What gives?" being informal for
"What is happening here?"), while the latter is formal style (as one would
expect in a scientific work, and which I would prefer for the FAQ).
Another, equally formal, but more programming-related expression would be
"returns", since we are talking about a return value of a method after all.

I am not a speaker of English as first language, though, and can only use
the written resources available to me to make that assessment, for example
<http://www.merriam-webster.com/dictionary/results> and <http://www.merriam-
webster.com/thesaurus/result>.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300...@news.demon.co.uk>

Dr J R Stockton

unread,
Oct 11, 2010, 2:30:57 PM10/11/10
to
In comp.lang.javascript message <4365431.e...@PointedEars.de>,
Sun, 10 Oct 2010 14:23:19, Thomas 'PointedEars' Lahn
<Point...@web.de> posted:

Thereby, you continue to demonstrate your lack of intelligence. One who
makes grammatical errors is not qualified to comment on language
matters. Leave that to well-educated native speakers of the English
language.

--
(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.

Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.

Thomas 'PointedEars' Lahn

unread,
Oct 12, 2010, 5:26:01 PM10/12/10
to
Dr J R Stockton wrote:

> Thomas 'PointedEars' Lahn posted:

I am still at a loss to find the right words for expressing how little your
unfounded opinion matters to me.

> One who makes grammatical errors is not qualified to comment on language
> matters. Leave that to well-educated native speakers of the English
> language.

Maybe you did not notice the relativating phrases that I have used. I have
used them intentionally.

And perhaps you could enlighten me as to the grammatical errors that you
think I have been making so far, so that I could avoid them in the future.
Or is it instead that you are only using this in yet another futile attempt
to appear superior?


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

0 new messages