AS3将数字转换为字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3将数字转换为字符串相关的知识,希望对你有一定的参考价值。

// 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. }

以上是关于AS3将数字转换为字符串的主要内容,如果未能解决你的问题,请参考以下文章

AS3将数字转换为单词

ActionScript 3 AS3将数字转换为单词

10个JavaScript代码片段,使你更加容易前端开发。

10个JavaScript代码片段,使你更加容易前端开发。

ActionScript 3 AS3将字符串转换为函数名称

将 AS2 代码转换为 AS3 格式