Prev: Firebug - how to retrieve/manipulate an object inspected in the DOM tab (similar to $0 for the HTML tab)?
Next: FAQ Topic - Why does K = parseInt('09') set K to 0? (2010-07-30)
From: Dr J R Stockton on 29 Jul 2010 15:57 In comp.lang.javascript message <k-idncXl5612ys3RnZ2dnUVZ_rqdnZ2d(a)gigane ws.com>, Wed, 28 Jul 2010 12:16:43, kangax <kangax(a)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.
From: Dr J R Stockton on 7 Aug 2010 15:57 In comp.lang.javascript message <i3huli$dkv$1(a)news.eternal- september.org>, Fri, 6 Aug 2010 14:26:23, Garrett Smith <dhtmlkitchen(a)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>.
From: Dr J R Stockton on 9 Aug 2010 13:14 In comp.lang.javascript message <i3lcr3$vjp$1(a)news.eternal- september.org>, Sat, 7 Aug 2010 21:46:40, Garrett Smith <dhtmlkitchen(a)gmail.com> posted: >On 2010-08-07 12:57 PM, Dr J R Stockton wrote: >> In comp.lang.javascript message<i3huli$dkv$1(a)news.eternal- >> september.org>, Fri, 6 Aug 2010 14:26:23, Garrett Smith >> <dhtmlkitchen(a)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. 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: Dr J R Stockton on 11 Aug 2010 18:05 In comp.lang.javascript message <i3qcta$uc3$1(a)news.eternal- september.org>, Mon, 9 Aug 2010 19:18:17, Garrett Smith <dhtmlkitchen(a)gmail.com> posted: >On 2010-08-09 10:14 AM, Dr J R Stockton wrote: >> In comp.lang.javascript message<i3lcr3$vjp$1(a)news.eternal- >> september.org>, Sat, 7 Aug 2010 21:46:40, Garrett Smith >> <dhtmlkitchen(a)gmail.com> posted: >> >>> On 2010-08-07 12:57 PM, Dr J R Stockton wrote: >>>> In comp.lang.javascript message<i3huli$dkv$1(a)news.eternal- >>>> september.org>, Fri, 6 Aug 2010 14:26:23, Garrett Smith >>>> <dhtmlkitchen(a)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". 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? -- (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: Dr J R Stockton on 13 Aug 2010 18:08
In comp.lang.javascript message <i4004v$djc$1(a)news.eternal- september.org>, Wed, 11 Aug 2010 22:17:21, Garrett Smith <dhtmlkitchen(a)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. |