AS3 Truncating Decimal Number


/ Published in: ActionScript 3
Save to your folder(s)

Examples of different ways to truncate/round a decimal number.


Copy this code and paste it in your HTML
  1. var num:Number = 98.76543210;
  2.  
  3. var num2:Number = Number(num.toFixed(2));
  4. trace("num2: " + num2);
  5. // OUTPUT
  6. // num2: 98.77
  7.  
  8. var num3:Number = Number(num.toFixed(1));
  9. trace("num3: " + num3);
  10. // OUTPUT
  11. // num3: 98.8
  12.  
  13. var num4:Number = Number(num.toFixed(0));
  14. trace("num4: " + num4);
  15. // OUTPUT
  16. // num4: 99
  17.  
  18. var num5:Number = Number(Math.abs(num));
  19. trace("num5: " + num5);
  20. // OUTPUT
  21. // num5: 98.7654321
  22.  
  23. var num6:Number = Number(int(num));
  24. trace("num6: " + num6);
  25. // OUTPUT
  26. // num6: 98
  27.  
  28. var num7:Number = Number(Math.round(num));
  29. trace("num7: " + num7);
  30. // OUTPUT
  31. // num7: 99
  32.  
  33. var num8:Number = Number(Math.floor(num));
  34. trace("num8: " + num8);
  35. // OUTPUT
  36. // num8: 98
  37.  
  38. var num9:Number = Number(Math.ceil(num));
  39. trace("num9: " + num9);
  40. // OUTPUT
  41. // num9: 99

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.