Robert Lozyniak
Robert Lozyniak

Reputation: 322

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?

Upvotes: 0

Views: 75

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074335

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.

Upvotes: 1

Related Questions