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 ファイル (以下を参照) を呼び出して、1000 単位の区切り文字 (英国ではコンマ、ドイツではピリオド (ピリオド) であり、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

ローカライズされた数値をサポートする組み込みの数値書式設定オプションを使用してみましたか: NumberFormatter によるローカライズされた書式設定

于 2014-03-13T17:26:50.593 に答える