myNumber.toFixed( [ fractionDigits ] ) in ECMAScript 262
Return the number formatted with a specified number of digits after the decimal point.
Arguments
name | type | description |
---|---|---|
fractionDigits | Number | [optional] The number of digits which sho |
- Return Type
- String
Description
If fractionDigits
is omitted, 0 is assumed (the same as Math.round(myNumber).toString()
).
var precision = [0,1,2,3];
var numbers = [ 0 , 0.567 , 1.5 , -0.07 ];
var msg="";
for (var i=0,len=numbers.length;i<len;i++){
var n = numbers[i];
for (var j=0,len2=precision.length;j<len2;j++){
var digits = precision[j];
msg += "("+n+").toFixed("+digits+") is '"+n.toFixed(digits)+"'\n";
}
}
/*********************************
msg is:
(0).toFixed(0) is '0'
(0).toFixed(1) is '0.0'
(0).toFixed(2) is '0.00'
(0).toFixed(3) is '0.000'
(0.567).toFixed(0) is '1'
(0.567).toFixed(1) is '0.6'
(0.567).toFixed(2) is '0.57'
(0.567).toFixed(3) is '0.567'
(1.5).toFixed(0) is '2'
(1.5).toFixed(1) is '1.5'
(1.5).toFixed(2) is '1.50'
(1.5).toFixed(3) is '1.500'
(-0.07).toFixed(0) is '-0'
(-0.07).toFixed(1) is '-0.1'
(-0.07).toFixed(2) is '-0.07'
(-0.07).toFixed(3) is '-0.070'
*********************************/