用于 Flash 的 AS3 flash 货币格式化程序

发布于 2024-11-02 23:45:45 字数 3689 浏览 0 评论 0原文

我需要帮助来扩展正确的字符串?将我的数字步进器和/或文本显示字段格式化为货币格式。您可以从此处的 CS5 flash FLA 下载 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 );  //??

I need help in extending the proper string?? to format my numericstepper and/or text display fields to currency formatted. You can download the FLA from CS5 flash FLA HERE. Thanks

 /* 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 );  //??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掀纱窥君容 2024-11-09 23:45:45

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

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

Hey there I hope this helps,

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

The function is pretty simple, we call the function formatCost()

If you just wish to format it to 0 decimals, formatCost(value as string,0)
If you need a currency add that in as the 3rd parameter.
If you need the symbol to appear behind the number, have the position set to 1
So...

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

if wanted, you can tack on more to the formula so that the 5th variable could be the spacer, so that you could change it from a "," to a "." (for french) or as a space " "

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
债姬 2024-11-09 23:45:45

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

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

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

    }

该类非常灵活,允许您自定义区域设置的货币外观。

A better alternative for currency formatting is to use the Adobe class flash.globalization.CurrencyFormatter

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

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

    }

This class is very flexible, allowing to you customize the currency appearance for locale.

人海汹涌 2024-11-09 23:45:45

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

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 没有价值 - 因此它仍然无法按预期运行,但这解决了眼前的问题。

The issue is that you are not paying attention to the variable types.

this.payrollOut -- is a textfield

this.payrollOut.text -- is the text contained in that textfield

this.payroll.value cannot be assigned a textfield or a text value

So you must convert to the type that it is expecting -- in this case a Number.

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

cf.format is expecting a Number value as well, and will return a String.

an example of usage might be :

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

There are other issues with the code that I see -- payrollOut has no value - so it's still not going to function as expected, but this solves the immediate problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文