Copy link to clipboard
Copied
Hi there,
My program makes some change.
Everything works well but not this amount : 2.05 $.
Instead of giving 5 cents, it gives 4 cents.
I don't understand why. This is my code.
Tks a lot.
Coucou.
package {
import flash.display.*;
import flash.text.*;
import flash.events.MouseEvent;
public class U1A4_Monnaie extends MovieClip {
private var monMessage:TextField=new TextField;
private var maMiseEnForme:TextFormat=new TextFormat;
private var montantAffichageFormat:TextFormat = new TextFormat;
public function U1A4_Monnaie():void {
btnSoumettre.addEventListener(MouseEvent.CLICK, calculeMonnaie);
}
private function calculeMonnaie(event:MouseEvent):void {
// Changement de l'apparence de la fenêtre
maMiseEnForme.font="Tahoma";
maMiseEnForme.size=14;
maMiseEnForme.color= 0x000000;
maMiseEnForme.leftMargin = 20;
maMiseEnForme.rightMargin = 20;
montantAffichageFormat.bold = true; // Format de l'affichage du montant total
monMessage.x=200;
monMessage.y=45;
monMessage.autoSize=TextFieldAutoSize.LEFT;
monMessage.defaultTextFormat=maMiseEnForme;
var somme:Number;
var totalCents:int;
var pieces200Cents:int=0; // Ajout des variables pour les pièces de 1$ et 2$
var pieces100Cents:int=0;
var pieces25Cents:int=0;
var pieces10Cents:int=0;
var pieces5Cents:int=0;
var pieces1Cent:int=0;
var restant:int=0;
var nouvMessage:String;
somme = Number(montant.text);
totalCents = int(somme * 100);
pieces200Cents = totalCents / 200; // Ajout du calcul du montant de pièces de 2$
restant= totalCents % 200;
pieces100Cents = restant / 100; // Ajout du calcul du montant de pièces de 1$
restant= totalCents % 100;
pieces25Cents = restant / 25;
restant= totalCents % 25;
pieces10Cents = restant / 10;
restant = restant % 10;
pieces5Cents = restant / 5;
restant = restant % 5;
pieces1Cent = restant;
montant.text=" ";
nouvMessage =("\n2 dollars: " + pieces200Cents + // Affichage des nouveaux montants
"\n1 dollar: " + pieces100Cents +
"\n25 cents: " + pieces25Cents +
"\n10 cents: " + pieces10Cents +
"\n5 cents: " + pieces5Cents +
"\n1 cent: " + pieces1Cent + "\n");
montantAffichage.defaultTextFormat = montantAffichageFormat;
montantAffichage.text = "$" + somme.toFixed(2).toString(); // Affichage du montant donné
monMessage.text=nouvMessage;
addChild(monMessage);
}
}
}
you're probably running into a binary arithmetic issue with your definition of totalCents. try using Math.round() instead of int.
Copy link to clipboard
Copied
you're probably running into a binary arithmetic issue with your definition of totalCents. try using Math.round() instead of int.
Copy link to clipboard
Copied
Tks a lot.
Have a great weekend.
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now