Comments on: How to format numbers in JavaScript flexibly and efficiently http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/ Stay curious! Thu, 02 May 2013 12:36:53 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Name http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-19442 Name Tue, 21 Jun 2011 11:50:44 +0000 http://www.xaprb.com/blog/?p=74#comment-19442 JavaScript has built-in methods to format a number to a certain precision. They are toFixed and toPrecision, and are part of the Number object. Any browser that supports ECMAScript version 3 should support toFixed and toPrecision. This roughly equates to Netscape 6.0 and above and IE 5.5 and above.

]]>
By: Felix http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-16565 Felix Wed, 17 Jun 2009 20:21:18 +0000 http://www.xaprb.com/blog/?p=74#comment-16565 Great!…realy usefull!, thanks

]]>
By: TQ http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-15683 TQ Fri, 16 Jan 2009 08:19:39 +0000 http://www.xaprb.com/blog/?p=74#comment-15683 THANK YOU!

]]>
By: Jeferson Zanim http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-15614 Jeferson Zanim Tue, 30 Dec 2008 16:58:14 +0000 http://www.xaprb.com/blog/?p=74#comment-15614 To correct the problem found by Heath change the old method by this new one:

Number.prototype.round = function(decimals) {
if (decimals > 0) {
var m = this.toFixed(decimals + 1).match(
new RegExp(“(-?\\d*)\.(\\d{” + decimals + “})(\\d)\\d*$”));
if (m && m.length) {
var mathRoundedValue = Math.round(m[2] + “.” + m[3]);
var paddedValue = String.leftPad(mathRoundedValue, decimals, “0″);
if (paddedValue.length > decimals) {
m[1] = ((new Number(m[1])) + 1).toString();
paddedValue = paddedValue.toString().substring(1);
}
return new Number(m[1] + “.” + paddedValue);
}
}
return this;
}

]]>
By: Abraham http://www.xaprb.com/blog/2006/01/05/javascript-number-formatting/#comment-15565 Abraham Mon, 15 Dec 2008 20:23:24 +0000 http://www.xaprb.com/blog/?p=74#comment-15565 We recently came across this great number-formatter JavaScript. We immediately jumped on to it and we are using it. However, our company requires that displaying and rounding to function in a similar way as that of MS Excel. To be more specific, we want 2.3 using the format #.#### to be formatted as 2.3 not as 2.3000. But 2.3 should be displayed as 2.3000 if the format is #.#000.

Does any body has a solution or a suggestion for this?
Thanks.

]]>