Bug in Number.prototype.toFixed for 0.5 and -0.5

Esben Andreasen esben at esbena.dk
Wed Sep 2 21:59:30 UTC 2015


The implementation of Number.prototype.toFixed seems to be wrong in
two cases. Can you confirm this, and/or report it on an issue tracker?

Observed behaviour:

> $ jjs -version
> nashorn 1.8.0_60
> jjs> 0.5.toFixed()
> 0
> jjs> -0.5.toFixed()
> 0

Expected behaviour:

The two results should be 1 and -1 respectively.

I expect this behaviour because that is how I interpret ES5 and ES6
specifications for Number.prototype.toFixed, and because that is the
behaviour that I can observe in Chrome and Firefox.



Extra comments on possible cause of bug:

The interesting part of the ES6 specification is

20.1.3.3 - 10. - a.:

> 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.

In the case of `0.5.toFixed()` the expression above becomes:

> n / 10^0 - 0.5 = n / 1 - 0.5 = n - 0.5

which solves to `n = 0` or `n = 1`.  It seems that Nashorn picks the
`n = 0` case instead of the `n = 1` case.



---
Esben Andreasen


More information about the nashorn-dev mailing list