Prev: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2010-07-28)
Next: Cairo& Giza sightseeing tour, quick overday trip shore excursion from Alexandria port
From: kangax on 28 Jul 2010 12:16 On 7/27/10 7:24 PM, Ry Nohryb wrote: > On Jul 28, 1:00 am, "FAQ server"<javascr...(a)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) 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
From: Garrett Smith on 6 Aug 2010 17:26 On 2010-07-29 04:53 PM, dhtml wrote: > On Jul 29, 1:03 pm, Dr J R Stockton<reply1...(a)merlyn.demon.co.uk> > wrote: >> In comp.lang.javascript message<4c4f64f9$0$279$14726...(a)news.sunsite.dk>, Tue, 27 Jul 2010 23:00:03, FAQ server<javascr...(a)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. 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
From: Garrett Smith on 8 Aug 2010 00:46 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". 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
From: Garrett Smith on 9 Aug 2010 22:18 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". > > 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
From: Garrett Smith on 12 Aug 2010 01:17
On 2010-08-11 03:05 PM, Dr J R Stockton wrote: > 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. > 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 |