javascripttofixed

Is behavior of Javascript toFixed() consistent between browsers? (leading zero and rounding)


I tried document.write((2/3).toFixed(3)); in JSFiddle, and it came out as 0.667. I am running Firefox on Windows, if that matters.

Will (2/3).toFixed(3) always give me "0.667", and never ".667" or "0.666" or ".666" or anything else? Are different browsers allowed to behave differently? Or may I rely on the presence of the leading zero, and rounding to nearest?


Solution

  • The behavior of toFixed is fully specified in the JavaScript specification and locale is not considered as part of those steps. As such, its output is consistent between conforming implementations of JavaScript engines.