簡體   English   中英

適用於Flash的AS3 Flash CurrencyFormatter

[英]AS3 flash currencyformatter for Flash

我需要擴展適當字符串的幫助嗎? 將我的數字步進器和/或文本顯示字段格式化為貨幣格式。 您可以從CS5 Flash FLA HERE下載FLA 謝謝

 /* start application */
import flash.geom.Matrix;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.text.TextField;
import flash.net.URLLoader;
import flash.globalization.CurrencyFormatter;


//comboBox, I populate values displayed to users

    this.comptype.addItem ( { label: "clerical" } );
    this.comptype.addItem ( { label: "concrete" } );
    this.comptype.addItem ( { label: "demolition" } );
    this.comptype.addItem ( { label: "electricians" } );
    this.comptype.addItem ( { label: "excavation" } );
    this.comptype.addItem ( { label: "HVAC/Plumbing" } );
    this.comptype.addItem ( { label: "oil and gas" } );
    this.comptype.addItem ( { label: "road/bridge construction" } );
    this.comptype.addItem ( { label: "roofing" } );
    this.comptype.addItem ( { label: "sewer construction" } );
    this.comptype.addItem ( { label: "truck driving" } );
    this.comptype.addItem ( { label: "warehousing" } );

this.comptype.addEventListener (Event.CHANGE, selectOrder); 
function selectOrder (event:Event) : void

    {
        if (this.comptype.selectedItem.label == "clerical") this.orderTotal.text = ("3.00");
        if (this.comptype.selectedItem.label == "concrete") this.orderTotal.text = ("8.00"); //make sure my values match the above values EXACTLY
        if (this.comptype.selectedItem.label == "demolition") this.orderTotal.text = ("10.00"); //make sure my values match the above values EXACTLY
        if (this.comptype.selectedItem.label == "electricians") this.orderTotal.text = ("5.00"); //make sure my values match the above values EXACTLY
        if (this.comptype.selectedItem.label == "excavation") this.orderTotal.text = ("8.00"); //make sure my values match the above values EXACTLY
        if (this.comptype.selectedItem.label == "HVAC/Plumbing") this.orderTotal.text = ("6.00"); //make sure my values match the above values EXACTLY
        if (this.comptype.selectedItem.label == "oil and gas") this.orderTotal.text = ("13.00"); //make sure my values match the above values EXACTLY
        if (this.comptype.selectedItem.label == "road/bridge construction") this.orderTotal.text = ("10.00"); //make sure my values match the above values EXACTLY
        if (this.comptype.selectedItem.label == "roofing") this.orderTotal.text = ("18.00"); //make sure my values match the above values EXACTLY
        if (this.comptype.selectedItem.label == "sewer construction") this.orderTotal.text = ("9.00"); //make sure my values match the above values EXACTLY
        if (this.comptype.selectedItem.label == "truck driving") this.orderTotal.text = ("13.00"); //make sure my values match the above values EXACTLY
        if (this.comptype.selectedItem.label == "warehousing") this.orderTotal.text = ("7.00"); //make sure my values match the above values EXACTLY

    }
this.outline.calcBtn.addEventListener (MouseEvent.CLICK, goCalc);
function goCalc (Event:MouseEvent) : void

            {
        this.outline.annualSavings.text = (this.staffrate.value-(14.15 + parseInt(this.orderTotal.text)))*this.payroll.value;
        //this.outline.docCostMonth.text = (this.timesPerDay.value * 260) / 12 * this.docProcCost.value;
        //this.outline.docCostYear.text = (this.outline.docCostMonth.text *12);
        //this.outline.annualSavings.text = ((this.procAutoSaving.value * this.outline.docCostYear.text) / 100);
        }


var cf:CurrencyFormatter = new CurrencyFormatter( "en_US" );
this.payroll.value = this.payrollOut;
cf.format( this.payrollOut.text );  //??

嘿,我希望這會有所幫助,

myTextField1.text = formatCost(String(4813.134),2,"$",0);// result: $ 4,813.13

該函數非常簡單,我們將其formatCost()函數。

如果只想將其格式化為0 formatCost(value as string,0) ,請使用formatCost(value as string,0)
如果您需要貨幣,則將其添加為第三個參數。
如果需要在數字后面顯示符號,請將位置設置為1
所以...

formatCost(String(8/35*100),1,"%",1);// will give "22.9 %"

如果需要,您可以進一步增加公式,以便第5個變量可以是分隔符,以便可以將其從“,”更改為“”。 (對於法語)或空格“”

public function formatCost(v:String, 
           decimal_places:int = 2, 
           currency_symbol:String = "", 
           placement:int = 0){
        v = String(Number(v).toFixed(decimal_places));
        var result:String = new String();
        if(decimal_places == 0){
        }else{
            result = v.substr(-1-decimal_places);
            v = v.substr(0, v.length-1-decimal_places);
        }
        while( v.length > 3 ){
            result = "," + v.substr(-3) + result;
            v = v.substr(0, v.length-3);
        }
        if(v.length > 0){
            if(placement == 0){
                result = currency_symbol + " " + v + result;
            }else if(placement == 1){
                result = v + result + " " + currency_symbol;
            }else{
                result = v + result;
            }
        }
        return result;
    }//closes formatCost

貨幣格式的一種更好的替代方法是使用Adobe類flash.globalization.CurrencyFormatter

public static function getDollars(value:Number):String{

        var targetFormatter:CurrencyFormatter = new CurrencyFormatter("en-US");
        return targetFormatter.format(value, true);

    }

此類非常靈活,可讓您自定義語言環境的貨幣外觀。

問題是您沒有注意變量類型。

this.payrollOut-是一個文本字段

this.payrollOut.text-是該文本字段中包含的文本

無法為this.payroll.value分配文本字段或文本值

因此,您必須將其轉換為期望的類型-在這種情況下為數字。

this.payroll.value = Number(this.payrollOut.text);

cf.format也需要Number值,並且將返回String。

用法示例可能是:

var payrollAmount:Number = 400;
var payrollCurrencyAmount:String = cf.format(payrollAmount);

我看到的代碼還有其他問題-payrollOut沒有價值-因此它仍然無法按預期運行,但這解決了緊迫的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM