toFixed Numerical Routine
 toFixed002view, link (19.7 KB, 550×250) source (14.5 KB)Fri, 7 May 04, 8:13pm UTC 
 

The function given here takes a number and returns it as a string with the specified number of digits. To use, copy the function Number.prototype.toFixed() and put it in the first frame of your movie, or inside any #initclip/#endinitclip pair. Then when you have a number x that you want in fixed format with N digits, use the following method:

output = x.toFixed(N);

Another example:

var x = Math.sin(1);
trace(x); // traces 0.841470984807897
var str = x.toFixed(3);
trace(str); // traces 0.841


The variable must be a number to access the function, so explicit conversion may be necessary:

userString = "0.33443";
var x = Number(userString);
trace(x.toFixed(2)); // traces 0.33