这个问题与我们在英国国家统计局开发的动画地图模板有关。它已被应用于许多数据集和地理位置,许多用途没有问题。例如,
http://www.ons.gov.uk/ons/interactive/vp3-census-map/index.html
http://www.statistica.md/pageview.php?l=ro&idc=390&id=3807
.fla调用一个支持的.as文件(见下文)来引入一千个分隔符(在英国一个逗号,在德国一个完整的句点(句点)定义为elsewhwere。
但是,我当前映射的数据集具有较大的负值,并且它认为下面的ORIGINAL HELPER FUNCTION不喜欢具有3,6,9或12(等)数字的负值。
例如,< - > -100到-999呈现NaN,100到NaN,999。这是因为这些值被识别为4位数长。它们正在分裂,引入了逗号,并且-ve标志被误解了。
我认为方法必须是使用绝对值,添加逗号然后(对于负值)之后再添加-ve符号。但到目前为止,适应性辅助功能的试验仅产生错误。 : - (
有人可以告诉我如何把-ve标志放回来吗?
非常感谢。
布鲁斯·米切尔
//原始的帮助功能:接受一个数字并返回如有必要附带数千个分离器的字符串
function addThouSep(num) {
/*
a. Acquire the number - 'myTrendValue' or 'myDataValue' - from function calcValues
b. Record it (still as a number) to data precision.
1. Turn dataORtrend into a string
2. See if there is a decimal in it.
3. If there isn't, just run the normal addThouSep.
4. If there is, run addThouSep just on the first bit of the string - then add the decimal back on again at the end.
*/
var myNum:Number = correctFPE(num); // Create number variable myNum and populate it with 'num'
// (myTrendvalue or myData Value from calcValues function) passed thru 'correctPFE'
var strNum:String = myNum+""; // Create string version of the dataORtrend number - so instead of 63, you get '63'
var myArray = strNum.split("."); // Create array representing elements of strNum, split by decimal point.
//trace(myArray.length); // How long is the array?
if (myArray.length==1) { // Integer, no decimal.
if (strNum.length < 4)//999 doesn't need a comma.
return strNum;
return addThouSep(strNum.slice(0, -3))+xmlData.thouSep+strNum.slice(-3);
}
else { // Float, with decimal
if (myArray[0].length < 4)//999 doesn't need a comma
return strNum;
return (addThouSep(myArray[0].slice(0, -3))+xmlData.thouSep+myArray[0].slice(-3)+"."+myArray[1]);
}
}
//适应性辅助功能:接受一个数字并返回如有必要附带数千个分离器的字符串
function addThouSep(num) {
/*
a. Acquire the number - 'myTrendValue' or 'myDataValue' - from function calcValues
b. Record it (still as a number) to data precision.
1. Turn dataORtrend into a string
2. See if there is a decimal in it.
3. If there isn't, just run the normal addThouSep.
4. If there is, run addThouSep just on the first bit of the string - then add the decimal back on again at the end.
*/
var myNum:Number = correctFPE(num); // Create number variable myNum and populate it with 'num'
// (myTrendvalue or myData Value from calcValues function) passed thru 'correctPFE'
var myAbsNum:Number = Math.abs(myNum); // ABSOLUTE value of myNum
var strNum:String = myAbsNum+""; // Create string version of the dataORtrend number - so instead of 63, you get '63'
var myArray = strNum.split("."); // Create array representing elements of strNum, split by decimal point.
//trace(myArray.length); // How long is the array?
if (myNum <0){ // negatives
if (myArray.length==1) { // Integer, no decimal.
if (strNum.length < 4)//999 doesn't need a comma.
return strNum;
return addThouSep(strNum.slice(0, -3))+xmlData.thouSep+strNum.slice(-3);
}
else { // Float, with decimal
if (myArray[0].length < 4)//999 doesn't need a comma
return strNum;
return (addThouSep(myArray[0].slice(0, -3))+xmlData.thouSep+myArray[0].slice(-3)+"."+myArray[1]);
}
}
else // positive
if (myArray.length==1) { // Integer, no decimal.
if (strNum.length < 4)//999 doesn't need a comma.
return strNum;
return addThouSep(strNum.slice(0, -3))+xmlData.thouSep+strNum.slice(-3);
}
else { // Float, with decimal
if (myArray[0].length < 4)//999 doesn't need a comma
return strNum;
return (addThouSep(myArray[0].slice(0, -3))+xmlData.thouSep+myArray[0].slice(-3)+"."+myArray[1]);
}
}
=============================================== ===================================
答案 0 :(得分:3)
如果您经常添加逗号(或需要支持带小数的数字),那么您可能需要一个高度优化的实用程序函数并使用简单的字符串操作:
public static function commaify( input:Number ):String
{
var split:Array = input.toString().split( '.' ),
front:String = split[0],
back:String = ( split.length > 1 ) ? "." + split[1] : null,
pos:int = input < 0 ? 2 : 1,
commas:int = Math.floor( (front.length - pos) / 3 ),
i:int = 1;
for ( ; i <= commas; i++ )
{
pos = front.length - (3 * i + i - 1);
front = front.slice( 0, pos ) + "," + front.slice( pos );
}
if ( back )
return front + back;
else
return front;
}
虽然不那么优雅但稳定且高效 - 你可以在我对类似问题的回答中找到比较套件https://stackoverflow.com/a/13410560/934195
答案 1 :(得分:1)
为什么不使用像我这样做过的简单的东西?
function numberFormat(input:Number):String
{
var base:String = input.toString();
base = base.split("").reverse().join("");
base = base.replace(/\d{3}(?=\d)/g, "$&,");
return base.split("").reverse().join("");
}
试验:
trace( numberFormat(-100) ); // -100
trace( numberFormat(5000) ); // 5,000
trace( numberFormat(-85600) ); // -85,600
说明:
.replace()
查找所有出现的三个数字,后跟另一个数字。我们使用$&,
作为替换,这基本上意味着将所有这些出现并用我们找到的值替换它,加上逗号。答案 2 :(得分:0)
您是否尝试使用支持本地化数字值的内置数字格式选项: Localized Formatting with NumberFormatter