toFixed

toFixed(number x, integer digits) → string string

Returns a string containing the number x* written with exactly *digits digits after the decimal point: toFixed(1.0, 2) returns "1.00", where string gives "1.0".

The number is rounded to the nearest value, and a half rounds away from zero: toFixed(2.5, 0) returns "3" and toFixed(-2.5, 0) returns "-3". Rounding applies to the number as string writes it, so toFixed(1.005, 2) returns "1.01". The result always uses a dot, never scientific notation, and never "-0.00".

toFixed(1.0, 2)          // "1.00"
toFixed(3.14159, 2)      // "3.14"
toFixed(2.999, 2)        // "3.00"
toFixed(0.1 + 0.2, 2)    // "0.30"
toFixed(12345678.9, 2)   // "12345678.90"
toFixed(-0.004, 2)       // "0.00"
toFixed(7, 3)            // "7.000"
(1.0).toFixed(2);      // "1.00"
(3.14159).toFixed(2);  // "3.14"
(1.005).toFixed(2);    // "1.00"
(-0.004).toFixed(2);   // "-0.00"
f"{1.0:.2f}"            # '1.00'
f"{3.14159:.2f}"        # '3.14'
format(1.005, ".2f")    # '1.00'
format(0.125, ".2f")    # '0.12'
Parameters
Return
Note