1

这个问题与我们在英国国家统计局开发的动画地图模板有关。它已被应用于许多数据集和地理,许多用途没有问题。例如,

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 文件(见下文)来引入千位分隔符(在英国是逗号,在德国是句号(句点)在 elsewwere 中定义。

但是,我当前正在映射的数据集具有较大的负值,并且结果表明下面的 ORIGINAL HELPER FUNCTION 不喜欢具有 3、6、9 或 12 位(等)数字的负值。

例如 -100 到 -999 呈现为 NaN,100 到 NaN,999。

这是因为这些值被识别为 4 位长。它们被拆分,引入了逗号,并且 -ve 符号被误解了。

我认为该方法必须是使用绝对值,添加逗号,然后(对于负值)然后添加 -ve 符号。但到目前为止,ADAPTED HELPER FUNCTION 的试验只产生了错误。:-(

谁能告诉我如何将 -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]); 
       }
}

==================================================== =================================

4

3 回答 3

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

于 2013-08-26T02:02:16.960 回答
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

解释:

  1. 将输入数字转换为字符串。
  2. 扭转它。
  3. 用于.replace()查找三个数字后跟另一个数字的所有匹配项。我们$&,用作替换,这基本上意味着将所有这些出现并用我们找到的值替换它,加上一个逗号。
  4. 再次反转字符串并返回它。
于 2013-06-20T22:41:59.063 回答
1

您是否尝试过使用支持本地化数字值的内置数字格式化选项: Localized Formatting with NumberFormatter

于 2014-03-13T17:26:50.593 回答