Format nombre(exel) dans php
Résolu
christophe.therrien
Messages postés
102
Date d'inscription
Statut
Membre
Dernière intervention
-
christophe.therrien Messages postés 102 Date d'inscription Statut Membre Dernière intervention -
christophe.therrien Messages postés 102 Date d'inscription Statut Membre Dernière intervention -
Bonjour, j'aimerais savoir si il est possible que lorsqu'une personne entre un chiffre du genre "1234567" dans un champ de texte dans un formulaire, lorsque la personne quitte la case du champ de texte, le nombre se met automatiquement en format nombre (comme dans excel) "1 234 567". SI oui comment?
Ps il faut aussi que je puisse utiliser le nombre comme une variable.
Merci
Ps il faut aussi que je puisse utiliser le nombre comme une variable.
Merci
A voir également:
- Format nombre(exel) dans php
- Format epub - Guide
- Format factory - Télécharger - Conversion & Codecs
- Hp usb disk storage format tool - Télécharger - Stockage
- Format apfs - Guide
- Format doc - Guide
11 réponses
Une piste:
vas voir ce site: https://www.toutjavascript.com/source/formater.html
c'est faire grâce au javascript, en affichant le code source tu as le code
bien sur il ta faudra l'adapter.
par contre pour ne pas perdre la valeur initiale, tu rajoutes un champ caché et avant de faire la transfo du format tu y charge la valeur initiale
Voilà j'espère avoir été assez clair ?
vas voir ce site: https://www.toutjavascript.com/source/formater.html
c'est faire grâce au javascript, en affichant le code source tu as le code
bien sur il ta faudra l'adapter.
par contre pour ne pas perdre la valeur initiale, tu rajoutes un champ caché et avant de faire la transfo du format tu y charge la valeur initiale
Voilà j'espère avoir été assez clair ?
D'abord merci alain_42, je comprend comment il fait, mais pour moi le javascript c'est du chinois et je ne suis pas capable de repérer les fonctions qui font ce que je veux. pourrait tu m'aider un peu plus?
Encore une fois merci.
christophe
Encore une fois merci.
christophe
Bon ben voilà, ça marche mais il faut cliquer à coté de l'input qd on a rentré le nombre
il faudrait voir avec d'autre évenements que le onChange
il faudrait voir avec d'autre évenements que le onChange
<html> <head> <script type="text/javascript" language="javascript"> function format(valeur,decimal,separateur) { // formate un chiffre avec 'decimal' chiffres après la virgule et un separateur var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; var val=Math.floor(Math.abs(valeur)); if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;} var val_format=val+""; var nb=val_format.length; for (var i=1;i<4;i++) { if (val>=Math.pow(10,(3*i))) { val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i)); } } if (decimal>0) { var decim=""; for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";} deci=decim+deci.toString(); val_format=val_format+"."+deci; } if (parseFloat(valeur)<0) {val_format="-"+val_format;} return val_format; } function afficher() { var valeur_entree=document.getElementById('entree').value; //on va memoriser la valeur initiale dans le champ cache document.getElementById('valeur_initiale').value=valeur_entree; //formatage de la valeur entrée var valeur_formatee=format(valeur_entree,0,' '); // nombre de decimale=0, separateur milliers =espace //affichage de cette nouvelle valeur dans l'input d'entrée document.getElementById('entree').value=valeur_formatee; } </script> </head> <body> <form name="form1" method="post" action="script_php_appele.php"> Entrez un nombre : <input type="text" name="entree" id="entree" value="" onChange="javascript:afficher();"> <!-- champ cache pour memoriser le nombre initial, le name de ce champ est celui qui te servira a le recuperer dans le script_php_appelle.php --> <input type="hidden" name="valeur_initiale" id="valeur_initiale" value=""><br /><br /> <input type="submit" name="envoyer" value="Suite"> </form> </body> </html>
d'accord merci, une autre question (lol) si j'ai plusieurs cases, faut-il que je place ce code pour chaque case ou simplement une fois
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
oui il faut modifier la fonction et lui passer des parametres en arguments
<html> <head> <script type="text/javascript" language="javascript"> function format(valeur,decimal,separateur) { // formate un chiffre avec 'decimal' chiffres après la virgule et un separateur var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; var val=Math.floor(Math.abs(valeur)); if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;} var val_format=val+""; var nb=val_format.length; for (var i=1;i<4;i++) { if (val>=Math.pow(10,(3*i))) { val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i)); } } if (decimal>0) { var decim=""; for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";} deci=decim+deci.toString(); val_format=val_format+"."+deci; } if (parseFloat(valeur)<0) {val_format="-"+val_format;} return val_format; } ///// fonction avec des arguments cette fois çi elle peut servir plusieurs fois function afficher(id_lire,id_ecrire) { var valeur_entree=document.getElementById(id_lire).value; //on va memoriser la valeur initiale dans le champ cache document.getElementById(id_ecrire).value=valeur_entree; //formatage de la valeur entrée var valeur_formatee=format(valeur_entree,0,' '); // nombre de decimale=0, separateur milliers =espace //affichage de cette nouvelle valeur dans l'input d'entrée document.getElementById(id_lire).value=valeur_formatee; } </script> </head> <body> <form name="form1" method="post" action="script_php_appele.php"> <!-- on passe les parametres id correspondant au champ a traiter a l'appel de la fonction --> Entrez un nombre : <input type="text" name="entree1" id="entree1" value="" onChange="javascript:afficher('entree1','valeur_initiale1');"><br /> Entrez un nombre : <input type="text" name="entree2" id="entree2" value="" onChange="javascript:afficher('entree2','valeur_initiale2');"><br /> Entrez un nombre : <input type="text" name="entree3" id="entree3" value="" onChange="javascript:afficher('entree3','valeur_initiale3');"><br /><br /> <!-- champ cache pour memoriser le nombre initial, le name de ce champ est celui qui te servira a le recuperer dans le script_php_appelle.php --> <input type="hidden" name="valeur_initiale1" id="valeur_initiale1" value=""> <input type="hidden" name="valeur_initiale2" id="valeur_initiale2" value=""> <input type="hidden" name="valeur_initiale3" id="valeur_initiale3" value=""> <input type="submit" name="envoyer" value="Suite"> </form> </body> </html>
c'est ce que j'ai pensé mais cela ne fonctionne pas
voici mon code
voici mon code
<html> <head> <link rel="icon" type="image/png" href="logo.png" /> <title>Calcul du Bénéfice par action</title> <style type="text/css"> <!-- .style1 {font-family: "Gill Sans"} .style3 { font-family: "Gill Sans"; font-size: 18px; font-weight: bold; } --> </style> <script type="text/javascript" language="javascript"> function format(valeur,decimal,separateur) { // formate un chiffre avec 'decimal' chiffres après la virgule et un separateur var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; var val=Math.floor(Math.abs(valeur)); if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;} var val_format=val+""; var nb=val_format.length; for (var i=1;i<4;i++) { if (val>=Math.pow(10,(3*i))) { val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i)); } } if (decimal>0) { var decim=""; for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";} deci=decim+deci.toString(); val_format=val_format+"."+deci; } if (parseFloat(valeur)<0) {val_format="-"+val_format;} return val_format; } ///// fonction avec des arguments cette fois çi elle peut servir plusieurs fois function afficher(id_lire,id_ecrire) { var valeur_entree=document.getElementById(id_lire).value; //on va memoriser la valeur initiale dans le champ cache document.getElementById(id_ecrire).value=valeur_entree; //formatage de la valeur entrée var valeur_formatee=format(valeur_entree,0,' '); // nombre de decimale=0, separateur milliers =espace //affichage de cette nouvelle valeur dans l'input d'entrée document.getElementById(id_lire).value=valeur_formatee; } </script> </head> <body> <span class="style3">Calcul Bénéfice par action</span> <?php if(!preg_match("#^[0-9]+$#", $_POST['entree1']) || !preg_match("#^[0-9]+$#", $_POST['entree2']) || !preg_match("#^[0-9]+$#", $_POST['entree3'])) { ?> <form method="post" action=""> <table width="475" border="0"> <tr> <td width="176"><span class="style1">Bénéfice net : </span></td> <td width="146"><input name="entree1" id="entree1" type="text" class="style1" value="0" onChange="javascript:afficher('entree1','valeur_initiale1');" /></td> </tr> <tr> <td><span class="style1">Dividende privilegié :</span></td> <td><input name="entree2" id="entree2" type="text" class="style1" value="0" onChange="javascript:afficher('entree2','valeur_initiale2');"/></td> </tr> <tr> <td><span class="style1">Nombre d'actions : </span></td> <td><input name="entree3" id="entree3" type="text" class="style1" value="0" onChange="javascript:afficher('entree3','valeur_initiale3');"/></td> </tr> <tr> <td colspan="2"><label> <input type="hidden" name="valeur_initiale1" id="valeur_initiale1" value="" /> <input type="hidden" name="valeur_initiale2" id="valeur_initiale2" value="" /> <input type="hidden" name="valeur_initiale3" id="valeur_initiale3" value="" /> </label> <input name="imageField" type="image" src="images/calculer.jpg" onClick="submit();" border="0" width="65" height="20" /> <input name="imageField" type="image" src="images/remiseazero.jpg" onClick="reset(); return false;" border="0" width="87" height="20" /> <span class="style1">Résultat : 0$</span></td> </tr> </table> <label> </label> <p><label></label> </form> <?php } else { $d = ($_POST['valeur_initiale1'] - $_POST['valeut2']) / $_POST['entree3']; echo 'Résultat :'.$d; } ?> </body> </html>
c'est ce que j'ai pensé mais cela ne fonctionne pas
voici mon code
merci
voici mon code
<html> <head> <link rel="icon" type="image/png" href="logo.png" /> <title>Calcul du Bénéfice par action</title> <style type="text/css"> <!-- .style1 {font-family: "Gill Sans"} .style3 { font-family: "Gill Sans"; font-size: 18px; font-weight: bold; } --> </style> <script type="text/javascript" language="javascript"> function format(valeur,decimal,separateur) { // formate un chiffre avec 'decimal' chiffres après la virgule et un separateur var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; var val=Math.floor(Math.abs(valeur)); if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;} var val_format=val+""; var nb=val_format.length; for (var i=1;i<4;i++) { if (val>=Math.pow(10,(3*i))) { val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i)); } } if (decimal>0) { var decim=""; for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";} deci=decim+deci.toString(); val_format=val_format+"."+deci; } if (parseFloat(valeur)<0) {val_format="-"+val_format;} return val_format; } ///// fonction avec des arguments cette fois çi elle peut servir plusieurs fois function afficher(id_lire,id_ecrire) { var valeur_entree=document.getElementById(id_lire).value; //on va memoriser la valeur initiale dans le champ cache document.getElementById(id_ecrire).value=valeur_entree; //formatage de la valeur entrée var valeur_formatee=format(valeur_entree,0,' '); // nombre de decimale=0, separateur milliers =espace //affichage de cette nouvelle valeur dans l'input d'entrée document.getElementById(id_lire).value=valeur_formatee; } </script> </head> <body> <span class="style3">Calcul Bénéfice par action</span> <?php if(!preg_match("#^[0-9]+$#", $_POST['entree1']) || !preg_match("#^[0-9]+$#", $_POST['entree2']) || !preg_match("#^[0-9]+$#", $_POST['entree3'])) { ?> <form method="post" action=""> <table width="475" border="0"> <tr> <td width="176"><span class="style1">Bénéfice net : </span></td> <td width="146"><input name="entree1" id="entree1" type="text" class="style1" value="0" onChange="javascript:afficher('entree1','valeur_initiale1');" /></td> </tr> <tr> <td><span class="style1">Dividende privilegié :</span></td> <td><input name="entree2" id="entree2" type="text" class="style1" value="0" onChange="javascript:afficher('entree2','valeur_initiale2');"/></td> </tr> <tr> <td><span class="style1">Nombre d'actions : </span></td> <td><input name="entree3" id="entree3" type="text" class="style1" value="0" onChange="javascript:afficher('entree3','valeur_initiale3');"/></td> </tr> <tr> <td colspan="2"><label> <input type="hidden" name="valeur_initiale1" id="valeur_initiale1" value="" /> <input type="hidden" name="valeur_initiale2" id="valeur_initiale2" value="" /> <input type="hidden" name="valeur_initiale3" id="valeur_initiale3" value="" /> </label> <input name="imageField" type="image" src="images/calculer.jpg" onClick="submit();" border="0" width="65" height="20" /> <input name="imageField" type="image" src="images/remiseazero.jpg" onClick="reset(); return false;" border="0" width="87" height="20" /> <span class="style1">Résultat : 0$</span></td> </tr> </table> <label> </label> <p><label></label> </form> <?php } else { $d = ($_POST['valeur_initiale1'] - $_POST['valeur_initiale2']) / $_POST['valeur_initiale3']; echo 'Résultat :'.$d; } ?> </body> </html>
merci
il faut prendre toutes les valeurs_initiales
et fais comme ça:
et fais comme ça:
<html> <head> <link rel="icon" type="image/png" href="logo.png" /> <title>Calcul du Bénéfice par action</title> <style type="text/css"> <!-- .style1 {font-family: "Gill Sans"} .style3 { font-family: "Gill Sans"; font-size: 18px; font-weight: bold; } --> </style> <script type="text/javascript" language="javascript"> function format(valeur,decimal,separateur) { // formate un chiffre avec 'decimal' chiffres après la virgule et un separateur var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; var val=Math.floor(Math.abs(valeur)); if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;} var val_format=val+""; var nb=val_format.length; for (var i=1;i<4;i++) { if (val>=Math.pow(10,(3*i))) { val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i)); } } if (decimal>0) { var decim=""; for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";} deci=decim+deci.toString(); val_format=val_format+"."+deci; } if (parseFloat(valeur)<0) {val_format="-"+val_format;} return val_format; } ///// fonction avec des arguments cette fois çi elle peut servir plusieurs fois function afficher(id_lire,id_ecrire) { var valeur_entree=document.getElementById(id_lire).value; //on va memoriser la valeur initiale dans le champ cache document.getElementById(id_ecrire).value=valeur_entree; //formatage de la valeur entrée var valeur_formatee=format(valeur_entree,0,' '); // nombre de decimale=0, separateur milliers =espace //affichage de cette nouvelle valeur dans l'input d'entrée document.getElementById(id_lire).value=valeur_formatee; } </script> </head> <body> <span class="style3">Calcul Bénéfice par action</span> <?php if(isset($_POST['poste'])){ //le formulaire a été posté, on verifie les champs if((isset($_POST['valeur_initiale1']) AND preg_match("#^[0-9]+$#",$_POST['valeur_initiale1'])) AND (isset($_POST['valeur_initiale2']) AND preg_match("#^[0-9]+$#",$_POST['valeur_initiale2'])) AND (isset($_POST['valeur_initiale3']) AND preg_match("#^[0-9]+$#",$_POST['valeur_initiale3']) AND $_POST['valeur_initiale3'] > 0)){ $d = ($_POST['valeur_initiale1'] - $_POST['valeur_initiale2']) / $_POST['valeur_initiale3']; $resultat='Résultat :'.$d; }else{ echo 'Veuillez remplir tous les champs avec des valeurs numériques !'; } } ?> <form method="post" action=""> <table width="475" border="0"> <tr> <td width="176"><span class="style1">Bénéfice net : </span></td> <td width="146"><input name="entree1" id="entree1" type="text" class="style1" value="0" onChange="javascript:afficher('entree1','valeur_initiale1');" /></td> </tr> <tr> <td><span class="style1">Dividende privilegié :</span></td> <td><input name="entree2" id="entree2" type="text" class="style1" value="0" onChange="javascript:afficher('entree2','valeur_initiale2');"/></td> </tr> <tr> <td><span class="style1">Nombre d'actions : </span></td> <td><input name="entree3" id="entree3" type="text" class="style1" value="0" onChange="javascript:afficher('entree3','valeur_initiale3');"/></td> </tr> <tr> <td colspan="2"><label> <input type="hidden" name="valeur_initiale1" id="valeur_initiale1" value="" /> <input type="hidden" name="valeur_initiale2" id="valeur_initiale2" value="" /> <input type="hidden" name="valeur_initiale3" id="valeur_initiale3" value="" /> </label> <input type="hidden" name="poste" id="poste" value="ok" /> <input name="imageField" type="image" src="images/calculer.jpg" onClick="submit();" border="0" width="65" height="20" /> <input name="reset_imageField" type="image" src="images/remiseazero.jpg" onClick="reset(); return false;" border="0" width="87" height="20" /> <span class="style1"><?php if(isset($resultat)) echo $resultat?></span></td> </tr> </table> <label> </label> <p><label></label> </form> </body> </html>