JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow Number Right Arrow toFixed()

toFixed()  Number  instance method

Description

Returns a string representation of the number in fixed notation.

  • When used and there are more digits to display than specified, the fractional return of the answer will be rounded appropriately.
  • Object types other than Number will throw a TypeError exception.

Syntax


Signature Description
aNumber.toFixed([numOfFractionalDigits])Returns a string representation of the number in fixed notation.

Parameters


Parameter Description
numOfFractionalDigitsAn optional integer specifying the number of fractional digits to display.
  • When omitted the number of fractional digits is treated as zero.
  • A range in the value 0-20 which may differ dependant upon the implementation.
  • The implementation may throw a RangeError exception when outside the implementation range.

Examples

The code below displays some fixed numerical values.


// Create an array for strings and populate.
var aNumber = 16.456456456;
var stringValue = new Array(4);
stringValue[0] = aNumber.toFixed(); 
stringValue[1] = aNumber.toFixed(2); // 2 decimal Places
stringValue[2] = aNumber.toFixed(4); // 4 decimal Places
stringValue[3] = aNumber.toFixed(6); // 6 decimal Places
alert(stringValue);

Press the button below to action the above code:

Related Tutorials

JavaScript Advanced Tutorials - Lesson 3 - Number




  

go to home page Homepage go to top of page Top