Truncating a Number | Setting The Precision of a Float | ActionScript 3. (AS3)
Last Updated on Friday, 04 June 2010 13:01 Written by Nicholas Dunbar
Truncating a Number or Floating Point in ActionScript 3.0 The Efficient Way
I have seen a few solutions for truncating a Number. Many of these solutions used to truncate a float or Number in ActionScript 3.0 (AS3) are just plain over complicated and not optimized. People are often using a technique where they are converting the float into a string and then removing the trailing decimals and then converting it back to a floating point number or the Number type. This works, but is inefficient. I suggest the following for setting the precision of a float:
public static function truncate(val:Number, decimalPlaces:uint):Number{
var multiplier:uint = Math.pow(10, decimalPlaces);
var truncatedVal:Number = val*multiplier;
if (newNumber > int.MAX_VALUE){
return Number(truncatedVal.toFixed(decimalPlaces));
}
return int(truncatedVal)/multiplier;
}
it uses integer operations instead of string operations so it is a fast truncate solution. And when the number is larger than an int it switches to the standard slower way using the toFixed value. I'm sure you could use binary operators to replace the devision inorder to further optimize the function.

Atif Bashir
said:
|
Following is the optimized function to set precision of a Number in ActionScript public static function setPrecision(val:Number, decimalPlaces:uint):Number { var multiplier:uint = Math.pow(10, decimalPlaces); var truncatedVal:Number = Math.round(val*multiplier); return truncatedVal/multiplier; } |
|
|
report abuse
vote down
vote up
|
Justin Carasick
said:
|
I see this in my Number variable -- 1.208746705405106E7 it's driving me bonkers and needs to be 6 decimal places. The number should look like this. 12087321.220717 |
|
|
report abuse
vote down
vote up
|