ActionScript/Javascript上的ToFixed函数
第一次在stackoverflow中,但是我真的需要帮助来重建这个字符串 因此,基本上它是在Actionscript中,我需要重建Millions-字符串,以输出为123m,即包含数百万,旁边有数千,因为当前它只显示1M。我听说toFixed会做这件事,但我似乎无法让它发挥我的作用 任何例子都会有帮助,谢谢ActionScript/Javascript上的ToFixed函数,javascript,actionscript-3,actionscript,Javascript,Actionscript 3,Actionscript,第一次在stackoverflow中,但是我真的需要帮助来重建这个字符串 因此,基本上它是在Actionscript中,我需要重建Millions-字符串,以输出为123m,即包含数百万,旁边有数千,因为当前它只显示1M。我听说toFixed会做这件事,但我似乎无法让它发挥我的作用 任何例子都会有帮助,谢谢 公共静态函数balanceToString(值:int):字符串 { var后缀:String=“”; var resultValue:int=value; 如果(值>=1000000)
公共静态函数balanceToString(值:int):字符串
{
var后缀:String=“”;
var resultValue:int=value;
如果(值>=1000000)
{
结果值=数学下限(结果值/1000000);
结果固定值(4);
后缀=“M”;
}
否则,如果(值>=100000)
{
结果值=数学下限(结果值/1000);
后缀=“K”;
}
返回“”+resultValue.toString()+后缀;
}
您正在将签名中的数字转换为整数
试着用一个数字代替
public static function balanceToString(value:Number):String
{
var suffix:String = "";
var resultValue:Number = value;
if (value >= 1000000)
{
resultValue = Math.floor(resultValue / 1000000);
resultValue.toFixed(4);
suffix = "M";
}
else if (value >= 100000)
{
resultValue = Math.floor(resultValue / 1000);
suffix = "K";
}
return "" + resultValue.toString() + suffix;
}
我猜是这样的 实施:
public static function balanceToString(value:int):String
{
var suffix:String = "";
var divisor:Number = 1;
var precision:Number = 0;
if (value >= 100000)
{
// This will display 123456 as 0.12M as well.
divisor = 1000000;
precision = 2;
suffix = "M";
}
else if (value >= 500)
{
// This will display 543 as 0.5K.
divisor = 1000;
precision = 1;
suffix = "K";
}
// This allows you to control, how many digits to display after
// the dot . separator with regard to how big the actual number is.
precision = Math.round(Math.log(divisor / value) / Math.LN10) + precision;
precision = Math.min(2, Math.max(0, precision));
// This is the proper use of .toFixed(...) method.
return (value / divisor).toFixed(precision) + suffix;
}
用法:
trace(balanceToString(12)); // 12
trace(balanceToString(123)); // 123
trace(balanceToString(543)); // 0.5K
trace(balanceToString(567)); // 0.6K
trace(balanceToString(1234)); // 1.2K
trace(balanceToString(12345)); // 12K
trace(balanceToString(123456)); // 0.12M
trace(balanceToString(1234567)); // 1.23M
trace(balanceToString(12345678)); // 12.3M
trace(balanceToString(123456789)); // 123M
谢谢你的回答!这导致了一个新的错误<代码>错误:找不到类型或类型不是编译时常量:double。公共静态函数balanceToString(value:double):字符串,我正试图用此站点上以前的答案解决此问题抱歉,我不太熟悉ActionScript。我相信数字就是你要找的类型。