D
dhtml
I've been looking at one of the FAQ entry on formatting numbers, or
"toFixed".
http://jibbering.com/faq/#formatNumber
Quote:
| 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: "Why does simple decimal arithmetic give strange
| results?" <URL: http://jibbering.com/faq/#binaryNumbers >
|
| N = Math.round(N*100)/100 only converts N to a Number of value close
| to a multiple of 0.01; but document.write(N) does not give trailing
| zeroes.
|
| ECMAScript Ed. 3.0 (JScript 5.5 [but buggy] and JavaScript 1.5)
| introduced Number.prototype.toFixed, the main problem with this is the
| bugs in JScript's implementation.
|
| Most implementations fail with certain numbers, for example 0.07. The
| following works successfully for M>0, N>0:
|
| function Stretch(Q, L, c) { var S = Q
| if (c.length > 0) {
| while (S.length < L) {
| S = c+S;
| }
| }
| return S;
| }
|
| function StrU(X, M, N) { // X>=0.0
| var T, S = String(Math.round(X * Number("1e" + N)));
| if (S.search && S.search(/\D/) != -1) {
| return '' + X;
| }
| with (String(Stretch(S, M+N, '0')))
| return substring(0, T = (length-N)) + '.' + substring(T);
| }
|
| function Sign(X) {
| return X > 0 ? "+" : X < 0 ? "-" : " ";
| }
|
| function StrS(X, M, N) {
| return Sign(X) + StrU(Math.abs(X), M, N);
| }
|
| Number.prototype.toFixed = function(n){
| return StrS(this, 1, n);
| };
I think it should not use |with|, should use more descriptive variable
names, should not modify Number.prototype, should only include a sign if
the sign is negative, and should use a more descriptive name, such as
toFixedString(n).
What do you guys think of this code?
I've also noticed that many implementations' toFixed (op, saf, ff)
return a number instead of a string. For example, typeof 1.1.toFixed(2)
is "number" in all three aforementioned browsers. ECMA 262 says it
should be a string[1].
Garrett
[1] http://bclary.com/2004/11/07/#a-15.7.4.5
"toFixed".
http://jibbering.com/faq/#formatNumber
Quote:
| 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: "Why does simple decimal arithmetic give strange
| results?" <URL: http://jibbering.com/faq/#binaryNumbers >
|
| N = Math.round(N*100)/100 only converts N to a Number of value close
| to a multiple of 0.01; but document.write(N) does not give trailing
| zeroes.
|
| ECMAScript Ed. 3.0 (JScript 5.5 [but buggy] and JavaScript 1.5)
| introduced Number.prototype.toFixed, the main problem with this is the
| bugs in JScript's implementation.
|
| Most implementations fail with certain numbers, for example 0.07. The
| following works successfully for M>0, N>0:
|
| function Stretch(Q, L, c) { var S = Q
| if (c.length > 0) {
| while (S.length < L) {
| S = c+S;
| }
| }
| return S;
| }
|
| function StrU(X, M, N) { // X>=0.0
| var T, S = String(Math.round(X * Number("1e" + N)));
| if (S.search && S.search(/\D/) != -1) {
| return '' + X;
| }
| with (String(Stretch(S, M+N, '0')))
| return substring(0, T = (length-N)) + '.' + substring(T);
| }
|
| function Sign(X) {
| return X > 0 ? "+" : X < 0 ? "-" : " ";
| }
|
| function StrS(X, M, N) {
| return Sign(X) + StrU(Math.abs(X), M, N);
| }
|
| Number.prototype.toFixed = function(n){
| return StrS(this, 1, n);
| };
I think it should not use |with|, should use more descriptive variable
names, should not modify Number.prototype, should only include a sign if
the sign is negative, and should use a more descriptive name, such as
toFixedString(n).
What do you guys think of this code?
I've also noticed that many implementations' toFixed (op, saf, ff)
return a number instead of a string. For example, typeof 1.1.toFixed(2)
is "number" in all three aforementioned browsers. ECMA 262 says it
should be a string[1].
Garrett
[1] http://bclary.com/2004/11/07/#a-15.7.4.5