Posted By

adrianparr on 02/08/10


Tagged

words english number readable String convert int formatted sentence


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

tspitzr
pixelit


AS3 Convert a Number to a String


 / Published in: ActionScript 3
 

URL: http://simplistika.com/convert-number-to-word-spelling/

// Example usage ...

trace(fNumToWord(56345));

// Output ... // Fifty-Six Thousand and Three Hundred and Forty-Five

  1. function fNumToWord(vNum:int):String
  2. {
  3. var mWords:Array = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"];
  4. mWords[30] = "Thirty";
  5. mWords[40] = "Forty";
  6. mWords[50] = "Fifty";
  7. mWords[60] = "Sixty";
  8. mWords[70] = "Seventy";
  9. mWords[80] = "Eighty";
  10. mWords[90] = "Ninety";
  11.  
  12. var v : int;
  13. var vWord : String;
  14. var vString : String;
  15.  
  16. vWord = "";
  17. if (vNum < 21) {
  18. vWord += mWords[vNum];
  19. }else if (vNum < 100) {
  20. vWord += mWords[10 * Math.floor(vNum / 10)];
  21. v = vNum % 10;
  22. vString = (String(vNum).substr( -1) == "0") ? " " : "-";
  23. if (v > 0) {
  24. vWord += vString + mWords[v];
  25. }
  26. } else if (vNum < 1000) {
  27. vString = (String(vNum).substr( -2) == "00") ? " Hundred" : " Hundred and";
  28. vWord += mWords[Math.floor(vNum / 100)] + vString;
  29. v = vNum % 100;
  30. if (v > 0) {
  31. vWord += " " + fNumToWord(v);
  32. }
  33. } else if (vNum < 1000000) {
  34. vString = (String(vNum).substr( -3) == "000") ? " Thousand" : " Thousand and";
  35. vWord += fNumToWord(Math.floor(vNum / 1000)) + vString;
  36. v = vNum % 1000;
  37. if (v > 0) {
  38. vWord += " ";
  39. if (v < 100)
  40. vWord += " ";
  41. vWord += fNumToWord(v);
  42. }
  43. } else {
  44. vString = (String(vNum).substr( -6) == "000000") ? " Million" : " Million and";
  45. vWord += fNumToWord(Math.floor(vNum / 1000000)) + vString;
  46. v = vNum % 1000000;
  47. if (v > 0) {
  48. vWord += " ";
  49. if (v < 100) {
  50. vWord += " ";
  51. }
  52. vWord += fNumToWord(v);
  53. }
  54. }
  55. return vWord;
  56. }

Report this snippet  

You need to login to post a comment.