Séparer les milliers des nombres en JS

kitchinaportiquo -  
 Saad X Belbsir -
Bonjour,

Je me demandais si quelqu'un savais comment mettre une séparation tous les milliers pour les grands nombres et ce, en javascript.

Exemple:
Transformer 1056420125 en 1 056 420 125

Merci d'avance, kitchinaportiquo

3 réponses

  1. Defouille Messages postés 404 Statut Membre 54
     
    Bonjour,

    voila une petite fonction javascript qui fait ce que tu veux :)
    il suffit de lui passer un nombre en paramètre, elle renvoi une chaine de caractères composé de chiffres et d'espaces.

    function lisibilite_nombre(nbr)
    {
    		var nombre = ''+nbr;
    		var retour = '';
    		var count=0;
    		for(var i=nombre.length-1 ; i>=0 ; i--)
    		{
    			if(count!=0 && count % 3 == 0)
    				retour = nombre[i]+' '+retour ;
    			else
    				retour = nombre[i]+retour ;
    			count++;
    		}
    		alert('nb : '+nbr+' => '+retour);
    		return retour;
    }

    7
    1. Saad X Belbsir
       
      <script type="text/javascript">
      var value = 1234567.5556;

      function Arrondir( nomber, nbApVirg ) {
      return ( parseInt(nomber * Math.pow(10,nbApVirg) + 0.5) ) / Math.pow(10,nbApVirg);
      }

      function formatMillier( nombre ) {
      var nbrArrnd = Arrondir(nombre, 2);
      return new Intl.NumberFormat().format( nbrArrnd );
      }

      console.log( formatMillier( value ) );

      </script>
      0
  2. kitchinaportiquo
     
    Magnifique, merci ;)
    4
    1. Defouille Messages postés 404 Statut Membre 54
       
      Avec plaisir :)
      0
    2. bronson
       
      Splendide, j'ai rarement résolu un pb aussi efficacement et rapidement !
      1 000 000 000 000 merci
      Cdlt
      0
  3. toutanne
     
    Bonjour à tous,

    J'ai récupéré le script ci dessous
    <?php
    
    $now = time();
    $start = mktime(0, 0, 0, 1, 24, 2007);
    $carbonsaving =((($now - $start) * 0.0058774) + 130000);
    $format = round($carbonsaving, 3);
    // in this example
    // $now = a unix timestamp of this very second
    // $start is the date that you want the counter to start from sent over //as a unix timestamp
    // $carbonsaving is the calculation that you want to perform to get //your base figure
    // i.e. total saving = ((date now - start date)* growth rate) + base rate
    // this gives us the starting saving all that needs to be done is increment it with javascript
    ?>
    
    <script type="text/javascript">
    
    
    
    // we need to import our server side variable into javascript to let it increment live
    
    var car = <?php print($format); ?>;
    var rou
    
    function incs()
    {
    car = car + 10.46;
    rou = Math.round(car*100)/100
    document.getElementById("carb").innerHTML=rou;
    }
    // what function incs does is take car and adds 0.01 to it
    //rou rounds the figure to 2 dp
    //the document.getElementById("carb") can refer to a <p> tag //<span> or whatever and just says with .innerHTML=rou; that the //value between the results of rou
    //hope this helps
    //Nicholas King
    //ecotricity
    </script>


    et j'aurais aimé utiliser la fonction ci dessus mais je ne sais pas comment l'implémenter dans mon script, n'étant pas développeur, est ce que quelqu'un pourrais m'aider svp ?

    d'avance merci pour votre aide
    2