| Format Number |
| |
A simple function to return an expression formatted as a number rounded off to the specified number of decimal places.
In fact, in PHP and ASP there is standard bulit in function, number_format & FormatNumber to perform this, but there seem to be none in Flash. Thus, i decided to write one and call it formatNum.
In my formatNum, you will need to pass 2 variables in:
i) the value to be formatted, in either string or number variables
ii) number of decimal places to be formatted

function formatNum(a:Object,dp:Number):Number{
var amount:String=String(Math.round(a*Math.pow(10,dp)));
var decimal:String=amount.slice(-dp);
var integer:String=amount.substring(0,amount.length-dp);
return parseFloat(integer+"."+decimal);
}
trace(formatNum(123.45678,2));
trace(formatNum("123.45678",3));
trace(formatNum(123.45678,2)+formatNum("123.45678",3));

suggestion
|
| |
| |