Lisandro
Lisandro

Reputation: 1

Setting input text components to show numbers with two decimals as 3.0

I´m trying to build an app in FLASH AS 3.0 to quickly calculate an invoice. I have done most of it but now I can´t make it show only two decimals instead of amounts with large decimals.

so far this is my code:

import fl.managers.StyleManager; 

this.stop();

var Format1:TextFormat = new TextFormat();
Format1.size = 17;
Format1.font = "Arial Rounded MT Bold";
Format1.align = "right";
StyleManager.setComponentStyle(TextInput, "textFormat", Format1);

borrar.addEventListener(MouseEvent.CLICK, escuchadorLimpiar);
function escuchadorLimpiar(event) {

a1.text="";
a2.text="";
a3.text="";
a4.text="";
b1.text="";
b2.text="";
b3.text="";
b4.text="";
c1.text="";
c2.text="";
c3.text="";
c4.text="";
subtotal.text="";
iva.text="";
total.text="";
}
a1.restrict="0-9";
a2.restrict="0-9";
a3.restrict="0-9";
a4.restrict="0-9";
b1.restrict="0-9";
b2.restrict="0-9";
b3.restrict="0-9";
b4.restrict="0-9";
c1.restrict="0-9";
c2.restrict="0-9";
c3.restrict="0-9";
c4.restrict="0-9";
subtotal.restrict="0-9";
iva.restrict="0-9";
total.restrict="0-9";

 calcular.addEventListener(MouseEvent.CLICK, onSumar1, false, 0, true);
 function onSumar1(evt:MouseEvent):void
 {
 c1.text = String(Number(a1.text) * Number(b1.text) / Number(escalar.text));
 c2.text = String(Number(a2.text) * Number(b2.text) / Number(escalar.text));
 c3.text = String(Number(a3.text) * Number(b3.text) / Number(escalar.text));
 c4.text = String(Number(a4.text) * Number(b4.text) / Number(escalar.text));
 subtotal.text = String(Number(c1.text) + Number(c2.text) + Number(c3.text) +          Number(c4.text));
 iva.text = String(Number(total.text) - (Number(subtotal.text)));
 total.text = String(Number(subtotal.text) * Number(escalar.text));
 }

I have an invisible input text containing a constant number (1.12) called escalar.

Any help?

Upvotes: 0

Views: 197

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

You can use toFixed() :

var number:Number = 1/3;
trace(number.toFixed(2));

With that said if you are dealing with money you need to be careful about floating point inaccuracies, numbers might not be what you expect. It might be better to use integers for the dollar and cent part or use some kind of library.

Upvotes: 3

Related Questions