problem:
Working with numbers (with decimals) might be a nightmare, as a simple division may create a huge number of decimals. The problem is not the calculation but the storing this kind of data in sql databases and the calculation consume a lot of cpu. In case you develop an application that you want to work very fast the solution is to round the number after a division or multiplication. solutions:
1. Convert a Number to String
toFixed property of Number
The following example shows how toFixed(3) returns a string that rounds to three decimal places.
var num:Number = 7.31343;
trace(num.toFixed(3)); // 7.313The following example shows how toFixed(2) returns a string that adds trailing zeroes.
var num:Number = 4;
trace(num.toFixed(2)); // 4.00
2. Round a Number (number -> number, no conversion)
Math.round(val:Number):Number
The silly thing is that this function doesn't get argument for how many decimals to you want. This round function rounds the 0.5 to 1.
3. Convert a Number to Int (rounding)
int(Math.round(0.665)) // returns 1
No comments:
Post a Comment