Truncating a Number | Setting The Precision of a Float | ActionScript 3. (AS3)

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.

Trackback(0)
Comments (2)add comment

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
September 28, 2010
Votes: +1

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
March 16, 2011
Votes: +0

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

security image
Write the displayed characters


busy