From: FAQ server on 26 May 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.7 to 6.50, and 6 to 6.00? Rounding of x.xx5 is unreliable, as most 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 `. 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")); 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 28 May 2010 15:37 In comp.lang.javascript message <4bfda7fb$0$278$14726298(a)news.sunsite.dk >, Wed, 26 May 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? >ECMA-262 3rd Edition introduced ` Number.prototype.toFixed `. >There are bugs in JScript's implementation with certain numbers, >for example ` 0.07 `. Should be 0.007, AISB. -- (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.
|
Pages: 1 Prev: Math.PI.toString(16) -> 3.243f6a8885a3 and back Next: ASCII equivalent. |