Prev: Cheap Wholesale Timberland Shoes Puma Paul Smith Converse Shoes etc Free shipping (www.vipchinatrade.com)
Next: It's a fact not lost on the opportunity to see yourself
From: Dr J R Stockton on 20 Apr 2010 13:05 In comp.lang.javascript message <hqgl62$ggc$1(a)news.eternal- september.org>, Sun, 18 Apr 2010 21:11:44, Garrett Smith <dhtmlkitchen(a)gmail.com> posted: >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? >>> >>> Rounding of x.xx5 is uncertain, as such numbers are not >> ^ most >>> represented exactly. > >What such numbers? All numbers with three decimal places of which the last is 5, except for those four which are multiples of an eighth. Actually, the rounding may be certain, but hard to predict without test. Change 'uncertain' to 'unreliable'. Because of that, change 6.5 to the more general 6.7. >>> 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. >> > >"Microsoft JScript" would be redundant. Only to one with the sort of thing that TL uses as a mind. In a written language, redundancy is a guard against undetected error. >> There is no problem with 0.07.toFixed(2) (review Subject line) and >> so the example should be 0.007. >> > >There is a problem with 0.07.toFixed(1), though as you point out, that >is only one decimal place. The example can be changed to 0.007, which >is also a problem, and which is more relevant to the question. As I have repeatedly said. The Subject says 2 decimal places. >>> 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. >> > >That line can be removed. > >> 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. >> > >`positiveNum`? No. Lengthy identifiers are an annoyance, without compensating benefit, in short routines. >| function toUnsignedString(positiveNum, digits) { >| var t, s = Math.round(positiveNum * Math.pow(10, digits)) + "", >| start, end; >| if (/\D/.test(s)) { >| return "" + positiveNum; >| } >| 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." >| } Lengthy identifiers may also be wrong. Pi to 3 digits is 3.14; 3.142 is pi to three decimal places. And the routine (with digits=3) handles negative numbers between -0.005 and 0.0 correctly, as is necessary. Example : JavaScript (1/7 + 1/7 + 1/7 + 1/7 + 1/7 + 1/7 + 1/7) - 1.0 is actually about -2e-16; but the routine needs to treat it as zero. -- (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) |