【问题标题】:AS3 flash currencyformatter for Flash用于 Flash 的 AS3 flash 货币格式化程序
【发布时间】:2011-04-24 13:51:12
【问题描述】:

我需要帮助来扩展正确的字符串??将我的 numericstepper 和/或文本显示字段格式化为货币格式。您可以从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 );  //??

【问题讨论】:

    标签: flash actionscript-3 formatting numericstepper


    【解决方案1】:

    嘿,我希望这会有所帮助,

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

    函数很简单,我们调用函数formatCost()

    如果您只想将其格式化为 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
    

    【讨论】:

      【解决方案2】:

      货币格式化的更好替代方法是使用 Adob​​e 类 flash.globalization.CurrencyFormatter

      public static function getDollars(value:Number):String{
      
              var targetFormatter:CurrencyFormatter = new CurrencyFormatter("en-US");
              return targetFormatter.format(value, true);
      
          }
      

      这个类非常灵活,允许您自定义语言环境的货币外观。

      【讨论】:

        【解决方案3】:

        问题是你没有注意变量类型。

        this.payrollOut -- 是一个文本字段

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

        this.payroll.value 不能分配文本字段或文本值

        因此,您必须转换为预期的类型——在本例中为数字。

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

        cf.format 也需要一个数字值,并将返回一个字符串。

        一个使用示例可能是:

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

        我看到的代码还有其他问题 - payrollOut 没有价值 - 所以它仍然无法按预期运行,但这解决了眼前的问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-08
          • 2016-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-29
          • 2016-04-25
          • 2012-05-31
          相关资源
          最近更新 更多