2010-02-10 37 views
7

¿Cómo convierto un entero en un valor de cadena? Esto debe ser fácil. "Ya chicos en SO son los mejores para explicar". Todavía estoy trabajando en estos contadores tontos.convertir un número entero en una cadena como 3

necesidad de unir juntos

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

CEROS marcador de posición

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

Obtener acceso de propiedad no definida, 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)); 
     } 
    } 

NO ERROR NOW, que se rompió de otra manera

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; 
} 

Ejemplos

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

si estoy entendiendo su Q correctamente, el ejemplo # 2 parece responder a ella ...? ¿Cuál es el problema que estás enfrentando? –

+0

Agregue los scripts juntos y juegue. 1120: Acceso a la propiedad indefinida myInt.toString(); "¿Qué es lo que me falta?" –

+0

¿Dónde está myInt definido antes de hacer el toString()? –

Respuesta

23

myInt.toString();

+0

Gracias hombre. Vigile este tema si tengo problemas. –

+0

Pensé que lo hizo. Mi error 1120: Acceso a propiedad indefinida myInt. toString(); "¿Ver edición he hecho?" –

+0

Quiero que lea el error y descubra qué está mal con eso. (No estoy tratando de ser un asno, solo necesita saber cómo depurar) Avíseme si no puedes resolverlo, y te lo diré. –

2

Yo uso 5 + "", cada vez que agrega "" (sin carácter), convierte cualquier cosa en una cadena y es fácil de recordar.

0

CONTADOR "texto dinámico" con solución valores cero
que lo pongo en nombre de Ed, un ser humano que me ayudó a través del teléfono. Era un problema con los argumentos de cadena y la sintaxis en 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); 
} 
6

Tenía la impresión de que AS3 tiene un método String() que forzará explícitamente una variable del tipo número en una Cadena. Los enteros se pueden convertir a números con la suficiente facilidad, y estoy bastante seguro de que se realizaría implícitamente en este caso.

 
text = String(number); 
0

muy simple ==>

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