将整数转换为字符串AS3

8

如何将整数转换为字符串? 这应该很容易。"你们SO的家伙最擅长解释了。"我还在努力解决这些愚蠢的计数器。

需要将这个连接在一起

//My counter project "sends to dynamic text field"
var timer:Timer = new Timer(10);  
var count:int = 0; //start at -1 if you want the first decimal to be 0  
var fcount:int = 0; 

timer.addEventListener(TimerEvent.TIMER, incrementCounter);  
timer.start();  


function incrementCounter(event:TimerEvent) {  
  count++;  
  //
  fcount=int(count*count/10000);//starts out slow... then speeds up 
  //
  var whole_value:int = int(fcount / 100); //change value 
  var tenths:int = int(fcount / 10) % 10;   
  var hundredths:int = int(fcount) % 10;   

  mytext.text = whole_value + " : " + tenths + hundredths;  
} 

ZEROS PLACEHOLDER

//Code for adding "zero placeholders"
function formatCount(i:int):String { 

    var fraction:int = i % 100; 
    var whole:int = i / 100; 

    return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" : "") + fraction; 
} 

function test():void { 
    for (var i:int = 1; i<100000; i += 3) { 
        trace(i + " -> " + formatCount(i)); 
    } 
} 

获取未定义属性的访问,myInt.toString();

该代码意思是在一个未定义变量上调用toString()方法导致报错。
//joined together
    var timer:Timer = new Timer(10);  
    var count:int = 0; //start at -1 if you want the first decimal to be 0  
    var fcount:int = 0; 

    timer.addEventListener(TimerEvent.TIMER, incrementCounter);  
    timer.start();  



     myInt.toString();
    function incrementCounter(event:TimerEvent) {  
      count++;  
      //
      fcount=int(count*count/10000);//starts out slow... then speeds up 
      //
      var whole_value:int = int(fcount / 100); //change value 
      var tenths:int = int(fcount / 10) % 10;   
      var hundredths:int = int(fcount) % 10;   

      mytext.text = whole_value + " : " + tenths + hundredths;  
    }   

    function formatCount(i:int):String {  

        var fraction:int = i % 100;  
        var whole:int = i / 100;  

        return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" : "") + fraction;  
    }  

    function test():void {  
        for (var i:int = 1; i<100000; i += 3) {  
            trace(i + " -> " + formatCount(i));  
        }  
    } 

现在没有错误,但是可能会以其他方式出错

var timer:Timer = new Timer(10);  
var count:int = 0; //start at -1 if you want the first decimal to be 0  
var fcount:int = 0; 

timer.addEventListener(TimerEvent.TIMER, incrementCounter);  
timer.start();  

function incrementCounter(event:TimerEvent) {  
  count++;  
  //
  fcount=int(count*count/10000);//starts out slow... then speeds up 
  //
  var whole_value:int = int(fcount / 100); //change value 
  var tenths:int = int(fcount / 10) % 10;   
  var hundredths:int = int(fcount) % 10;   
////////////// 
 function formatCount(i:int):String { 

    var fraction:int = i % 100; 
    var whole:int = i / 100; 

    return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" : "") + fraction; 
} 

function test():void { 
    for (var i:int = 1; i<100000; i += 3) { 
        trace(i + " -> " + formatCount(i)); 
    } 
} 
//////////////
mytext.text = formatCount(whole_value + " : " + tenths + hundredths); 

 // mytext.text = whole_value + " : " + tenths + hundredths;  
}

例子

// string to number
var myString:String = "5";
var myNumber:Number = Number(myString);

// number to string
var myNumber:Number= 5;
var myString:String= String(myNumber);

// string to int (integer)
var myString:String = "5";
var myInt:int = int(myString);

如果我正确地理解了你的问题,第二个例子似乎已经回答了它...?你遇到了什么问题呢? - David Hedlund
将脚本添加在一起并播放。1120:访问未定义的属性myInt.toString(); “我错过了什么?” - anon255058
在你执行toString()之前,myInt在哪里被定义了? - user195488
5个回答

24

myInt.toString();


以为这样就可以了,但是出现了错误1120:访问未定义的属性myInt.toString(); “看到我所做的修改了吗?” - anon255058
我希望你能阅读错误信息并找出问题所在。(我不是故意刁难你,只是你需要知道如何调试)如果你无法解决问题,请告诉我,我会告诉你。 - Tyler Egeto

6

我的理解是,AS3中有一个String()方法,该方法可以将数字类型的变量明确转换为字符串。整数可以很容易地转换为数字,我非常确定在这种情况下会隐式地进行转换。

text = String(number);

2
我使用5 + "",任何时候添加""(没有字符),它都会将任何东西转换为字符串,并且很容易记住。

0

使用零值解决“动态文本”计数器问题
我代表Ed发帖,他在电话中帮助了我。这是一个关于字符串参数和mytext语法的问题。

//CA, NC, LONDON, ED "increments"
var timer:Timer = new Timer(10);  
var count:int = 0; //start at -1 if you want the first decimal to be 0  
var fcount:int = 0; 

timer.addEventListener(TimerEvent.TIMER, incrementCounter);  
timer.start();  

function incrementCounter(event:TimerEvent) {  
  count++;  
  fcount=int(count*count/10000);//starts out slow... then speeds up 
  mytext.text = formatCount(fcount);
}

function formatCount(i:int):String { 
     var fraction:int = i % 100; 
     var whole:int = i / 100; 

    return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" + fraction : fraction); 
} 

0

非常简单 ==>

var myint:int = 500;
var myintToString:String = myint+"";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接