Actionscript Round and keep decimals function

ActionScript:
  1. function formatDecimals2 (num, digits) {
  2.  
  3. if (num < 0 ) {
  4. num = 0;
  5. }
  6.  
  7. if (digits <= 0) {
  8. return Math.round(num);
  9. }
  10.  
  11. var tenToPower = Math.pow(10, digits);
  12. var cropped = String(Math.round(num * tenToPower) / tenToPower);
  13.  
  14. trace("digits:" + digits + " - " + cropped);
  15.  
  16. }
  17.  
  18. formatDecimals2( 35435.234234, 2);
  19. formatDecimals2( 35435.234234, 3);
  20. formatDecimals2( 35435.234234, 4);

Leave a Reply