.toFixed() bug in MS JScript script engine

That's a ridiculous argument. If toFixed(0) were supposed to return *unrounded*

values, (0.95).toFixed() would equal (0.94).toFixed(), just as (1.95).toFixed()

exactly equals (1.94).toFixed().

And I'm glad you brought up ECMA, because we can step through the ECMA

specification with (0.94).toFixed():

: Number.prototype.toFixed (fractionDigits)

: Return a string containing the number represented in fixed-point

: notation with fractionDigits digits after the decimal point. If

: fractionDigits is undefined, 0 is assumed. Specifically, perform

: the following steps:

:

: 1. Let f be ToInteger(fractionDigits). (If fractionDigits is

: undefined, this step produces the value 0).

f = 0

: 2. If f < 0 or f > 20, throw a RangeError exception.

: 3. Let x be this number value.

x = 0.94

: 4. If x is NaN, return the string "NaN".

: 5. Let s be the empty string.

s = ""

: 6. If x >= 0, go to step 9.

Skipping to step 9...

: 7. Let s be "-".

: 8. Let x = -x.

: 9. If x >= 10^21, let m = ToString(x) and go to step 20.

0.94 < 10^21, so continue...

: 10. Let n be an integer for which the exact mathematical value of

: n/10^f - x is as close to zero as possible. If there are two

: such n, pick the larger n.

n/10^0 = n/1 = n, which must be the closest integer to 0.94. This is the

rounding step. Since f is 0, this says "pick the closest integer to x, rounding

up". Thus, n = 1.

: 11. If n = 0, let m be the string "0". Otherwise, let m be the

: string consisting of the digits of the decimal representation

: of n (in order, with no leading zeroes).

m = "1"

: 12. If f = 0, go to step 20.

f = 0, so let's go to step 20...

: 13. Let k be the number of characters in m.

: 14. If k > f, go to step 18.

: 15. Let z be the string consisting of f+1-k occurrences of the

: character "0".

: 16. Let m be the concatenation of strings z and m.

: 17. Let k = f + 1.

: 18. Let a be the first kCf characters of m, and let b be the

: remaining f characters of m.

: 19. Let m be the concatenation of the three strings a, ".",

: and b.

: 20. Return the concatenation of the strings s and m.

return value: ("" + "1")

Thus, JScript does not conform to the ECMA 262 spec for Number.toFixed(). Could

not be more evident.

Quote:

> Why would you think that? Both the JScript documentation and the ECMAScript

> standards indicate that no rounding as you suggest occurs. The method

> Number.toFixed ( ) (from the standard 15.7.4.5):

> "Returns a string containing the number represented in fixed-point notation

> with fractionDigits digits after the decimal point. If fractionDigits is

> undefined, 0 is assumed."

> The integer portion of 0.94 is "0" (see the standard above, step 11). The

> fractional part is "94", but with zero digits showing it translates to "". 0

> + "" is "0".

This is where you read the spec incorrectly. Step 11 does not look at the

integer portion of x, it looks at the computed value n, which is 1.

Quote:

> Perhaps Math.ceil (0.94) will return the value he wants...

> But, toFixed does not appear to have a "bug".

On the contrary, it has a demonstrated and illustrated bug.

--

Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use of

this email address implies consent to these terms. Please do not contact me

directly or ask me to contact you directly for assistance. If your question is

worth asking, it's worth posting.