View Single Post
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator 17 Creative Assets 2006-11-19 #33 Old  
030 - Rounding Numbers
 
Here is a little utility function that you can use to round numbers to a certain decimal point.

It is very easy to use so I'll just drop some example code here...

ActionScript Code:
  1. function roundTo(value:Number, points:Number):Number
  2. {
  3.     if (points == null || points < 1)
  4.     {
  5.         return value >> 0;
  6.     }
  7.     var delta:Number = Math.pow(10, points);
  8.    
  9.     return (value * delta >> 0) / delta;
  10. }
  11.  
  12. //
  13.  
  14. var num:Number = 100.24680;
  15.  
  16. trace( roundTo(num) );    // output: 100
  17. trace( roundTo(num, 0) ); // output: 100
  18. trace( roundTo(num, 1) ); // output: 100.2
  19. trace( roundTo(num, 2) ); // output: 100.24
  20. trace( roundTo(num, 3) ); // output: 100.246
  21. trace( roundTo(num, 4) ); // output: 100.2468
  22. trace( roundTo(num, 5) ); // output: 100.23680
  23.