将整数转换为字符串as3

时间:2010-02-10 15:26:58

标签: flash actionscript-3 string integer

如何将整数转换为字符串值? 这一定很容易。 “在这方面,雅人最擅长解释。”我还在研究这些愚蠢的柜台。

需要一起加入

//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();

//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);

5 个答案:

答案 0 :(得分:23)

myInt.toString();

答案 1 :(得分:6)

我的印象是AS3有一个String()方法,该方法将类型号的变量显式强制转换为String。整数可以很容易地转换为数字,我很确定在这种情况下它会隐式完成。

text = String(number);

答案 2 :(得分:2)

我使用5 + "",无论何时添加""(无字符),它都会将任何内容转换为字符串,并且很容易记住。

答案 3 :(得分:0)

COUNTER“DYNAMIC TEXT”零值解决方案
我是代表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); 
} 

答案 4 :(得分:0)

非常简单==&gt;

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