Formater l'affichage d'un nombre

Résolu
oobox Messages postés 5 Date d'inscription vendredi 19 mars 2004 Statut Membre Dernière intervention 15 septembre 2008 - 12 sept. 2008 à 21:12
oobox Messages postés 5 Date d'inscription vendredi 19 mars 2004 Statut Membre Dernière intervention 15 septembre 2008 - 15 sept. 2008 à 16:45
Bonjour,
Je commence en flash et je recherche la methode pour tout simplement formater à l'affichage un nombre en ajoutant des espace du style 1 000 000

function ajouter() {
total = Math.round (a + b)
}

a = 500000;
b = 550000;

et j'affiche le total dans un var
mais le resultat affiche 1500000 et non pas 1 500 000
merci pour vos conseils

4 réponses

Skev Messages postés 255 Date d'inscription mardi 27 juin 2006 Statut Membre Dernière intervention 9 janvier 2010 3
15 sept. 2008 à 16:16
C'est normal, dans ma fonction j'utilise deux variable ( "total_format" et "total_string"), or toi tu ne les declares, ni utiliser nul part.
A la limite on pourrait le simplifier comme ceci :

//Code
function ajouter() {
    total = Formate(Math.round(a+b));
}
a = 500000;
b = 550000;
//
function Formate(total_non_format) {
    //
    total_string = new String(total_non_format);
    //
    if (total_string.length>9) {
        total_format = total_string.slice(0, -9)+" "+total_string.slice(total_string.length-9, -6)+" "+total_string.slice(total_string.length-6, -3)+" "+total_string.slice(total_string.length-3);
    } else if (total_string.length>6) {
        total_format = total_string.slice(0, -6)+" "+total_string.slice(total_string.length-6, -3)+" "+total_string.slice(total_string.length-3);
    } else if (total_string.length>3) {
        total_format = total_string.slice(0, -3)+" "+total_string.slice(total_string.length-3);
    } else if (total_string.length<=3) {
        total_format = total_string;
    }
    return (total_format);
}
//
ajouter();
//
trace(total);
3
Skev Messages postés 255 Date d'inscription mardi 27 juin 2006 Statut Membre Dernière intervention 9 janvier 2010 3
15 sept. 2008 à 14:55
Bonjour,
J'avais creer une fonction simmilaire pour un jeux , l'argent etait trop peu lisible,
Je te met mon code melangé a ta fonction :

///////////////////Code
function ajouter() {
    total = Math.round(a+b);
}
a = 500000;
b = 550000;
//
ajouter();
//
total_string = new String(total);
//
function Formate() {
    if (total_string.length>9) {
        total_format = total_string.slice(0, -9)+" "+total_string.slice(total_string.length-9, -6)+" "+total_string.slice(total_string.length-6, -3)+" "+total_string.slice(total_string.length-3);
    } else if (total_string.length>6) {
        total_format = total_string.slice(0, -6)+" "+total_string.slice(total_string.length-6, -3)+" "+total_string.slice(total_string.length-3);
    } else if (total_string.length>3) {
        total_format = total_string.slice(0, -3)+" "+total_string.slice(total_string.length-3);
    } else if (total_string.length<=3) {
        total_format = total_string;
    }
}
Formate();
//
trace(total_format);
///////////////////

Rq : il peut être optimiser je pense, il fait carrément brouillon !
0
oobox Messages postés 5 Date d'inscription vendredi 19 mars 2004 Statut Membre Dernière intervention 15 septembre 2008
15 sept. 2008 à 15:31
Merci pour ta réponse
je vais essayer d'intégrer ton code.

form:
1 texte de saisie Var: montant
1 Texte dynamique Var : total

Actions :

function ajouter() {
  total = Formate(montant * 10);
}
// EST CE QUE CETTE FONCTION PEUT FONCTIONNER AINSI ?
// Formate(montant * 10) ?


function Formate() {
    if (total_string.length>9) {
        total_format = total_string.slice(0, -9)+" "+total_string.slice(total_string.length-9, -6)+" "+total_string.slice(total_string.length-6, -3)+" "+total_string.slice(total_string.length-3);
    } else if (total_string.length>6) {
        total_format = total_string.slice(0, -6)+" "+total_string.slice(total_string.length-6, -3)+" "+total_string.slice(total_string.length-3);
    } else if (total_string.length>3) {
        total_format = total_string.slice(0, -3)+" "+total_string.slice(total_string.length-3);
    } else if (total_string.length<=3) {
        total_format = total_string;
    }
}

mais il y a un bug car cela ne fonctionne pas correctement

Merci
Pascal
0
oobox Messages postés 5 Date d'inscription vendredi 19 mars 2004 Statut Membre Dernière intervention 15 septembre 2008
15 sept. 2008 à 16:45
Merci pour ta collaboration
0
Rejoignez-nous