How to deal with Number precision in Actionscript?

This is my generic solution for the problem (I have blogged about this here): var toFixed:Function = function(number:Number, factor:int) { return Math.round(number * factor)/factor; } For example: trace(toFixed(0.12345678, 10)); //0.1 Multiply 0.12345678 by 10; that gives us 1.2345678. When we round 1.2345678, we get 1.0, and finally, 1.0 divided by 10 equals 0.1. Another example: … Read more

What are the major performance hitters in AS3 aside from rendering vectors?

Documents I’ve found helpful are: Optimizing Performance for the Adobe Flash Platform ActionScript 3.0 and AVM2 Performance Tuning by Gary Grossman Building High Performance iPhone Applications by Mike Chambers Some highlights: Choose appropriate display objects One of the most simple optimization tips to limit memory usage is to use the appropriate type of display object. … Read more

What is the best way to get the minimum or maximum value from an Array of numbers?

The theoretical answers from everyone else are all neat, but let’s be pragmatic. ActionScript provides the tools you need so that you don’t even have to write a loop in this case! First, note that Math.min() and Math.max() can take any number of arguments. Also, it’s important to understand the apply() method available to Function … Read more

Insert commas into number string

If your language supports postive lookahead assertions, then I think the following regex will work: (\d)(?=(\d{3})+$) Demonstrated in Java: import static org.junit.Assert.assertEquals; import org.junit.Test; public class CommifyTest { @Test public void testCommify() { String num0 = “1”; String num1 = “123456”; String num2 = “1234567”; String num3 = “12345678”; String num4 = “123456789”; String regex … Read more

Calculate Bounding box coordinates from a rotated rectangle

Transform the coordinates of all four corners Find the smallest of all four x’s as min_x Find the largest of all four x’s and call it max_x Ditto with the y’s Your bounding box is (min_x,min_y), (min_x,max_y), (max_x,max_y), (max_x,min_y) AFAIK, there isn’t any royal road that will get you there much faster. If you are … Read more