Reputation: 11
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 ); //??
Upvotes: 1
Views: 2365
Reputation: 8604
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.
Upvotes: 1
Reputation: 21
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
Upvotes: 2
Reputation: 6751
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.
Upvotes: 0