string 将整数转换为字符串 as3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2237940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert an integer to a string as3
提问by Tyler Egeto
How do I convert an integer to a string value?This must be easy. "Ya guys in SO are da best at explaining." I'm still working on these dumb counters.
如何将整数转换为字符串值?这一定很容易。“SO 里的你们最擅长解释了。” 我仍在研究这些愚蠢的计数器。
NEED TO JOIN THIS TOGETHER
需要一起加入
//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));
}
}
Getting access of undefined property, myInt.toString();
访问未定义的属性 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, BROKE IT SOME OTHER WAY
现在没有错误,以其他方式打破它
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;
}
EXAMPLES
例子
// 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);
回答by Tyler Egeto
myInt.toString();
myInt.toString();
回答by revenantphoenix
I was under the impression AS3 has a String() method which will explicitly coerce a variable of the type number into a String. Integers can be converted to numbers easily enough, and i'm pretty sure it would be done implicitly in this case.
我的印象是 AS3 有一个 String() 方法,它会显式地将类型号的变量强制转换为字符串。整数可以很容易地转换为数字,我很确定在这种情况下它会隐式完成。
text = String(number);
回答by Daniel
I use 5 + ""
, any time you add ""
(no character) , it converts anything to a string and it's easy to remember.
我使用5 + ""
, 任何时候添加""
(no character) ,它都会将任何内容转换为字符串,并且很容易记住。
回答by Daniel
COUNTER "DYNAMIC TEXT" with zero values solution
I'm posting on behalf of Ed, a human that helped me over the phone. It was a problem with the string arguments and the syntax in mytext.
带有零值解决方案的计数器“动态文本”解决方案
我代表 Ed 发布,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);
}
回答by Jakir Hossen
very simple ==>
很简单==>
var myint:int = 500;
var myintToString:String = myint+"";