Prev: why IE reset focus behaves different then firerfox
Next: How to control (disable/enable) all onclick events
From: FAQ server on 28 Jan 2010 19:00 ----------------------------------------------------------------------- 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 25 Feb 2010 16:55 In comp.lang.javascript message <4b62250a$0$272$14726298(a)news.sunsite.dk >, Fri, 29 Jan 2010 00: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? It is better not to use variables I J K L M N i j k l m n for things which are not essentially of integer value. Likewise for identifiers of more than one "arbitrary" character. There is little advantage in applying the rule for "self-explanatory" identifiers, though it is often easy enough to do so. -- (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. I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt; free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/> unsupported.
From: John G Harris on 26 Feb 2010 09:01 On Thu, 25 Feb 2010 at 21:55:00, in comp.lang.javascript, Dr J R Stockton wrote: >In comp.lang.javascript message <4b62250a$0$272$14726298(a)news.sunsite.dk >>, Fri, 29 Jan 2010 00: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? > >It is better not to use variables I J K L M N i j k l m n for things >which are not essentially of integer value. Likewise for identifiers of >more than one "arbitrary" character. There is little advantage in >applying the rule for "self-explanatory" identifiers, though it is often >easy enough to do so. FORTRAN lives! :-( John -- John Harris
From: Dr J R Stockton on 27 Feb 2010 16:27
In comp.lang.javascript message <ZcvHg0CFR9hLFwtC(a)J.A830F0FF37FB96852AD0 8924D9443D28E23ED5CD>, Fri, 26 Feb 2010 14:01:41, John G Harris <john(a)nospam.demon.co.uk> posted: >On Thu, 25 Feb 2010 at 21:55:00, in comp.lang.javascript, Dr J R >Stockton wrote: >>In comp.lang.javascript message <4b62250a$0$272$14726298(a)news.sunsite.dk >>>, Fri, 29 Jan 2010 00: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? >> >>It is better not to use variables I J K L M N i j k l m n for things >>which are not essentially of integer value. Likewise for identifiers of >>more than one "arbitrary" character. There is little advantage in >>applying the rule for "self-explanatory" identifiers, though it is often >>easy enough to do so. > >FORTRAN lives! :-( It is permissible to copy useful conventions from elsewhere, even if they are not mandatory here. But by exhibiting a knowledge of FORTRAN, you may be taken as showing either your age or a nerdish character. As it happens, I have before me, between keyboard and screen, an /echt/ FORTRAN card - its presence resulting from a recent discussion on the merits of using a fixed-pitch font for code on Web pages, and my contribution relating an initially hard-to-understand bug in a colleague's HP FORTRAN program in the post-card era (an algorithmically essential, but syntactically unnecessary, part of a statement was in columns 73-80 of the screen). -- (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. |