»
AnglaisFran�aisVietnamien

Imprimer - Ajouter Comas Pour les objets numériques - JavaScriptBank.com

Version compl�te: jsB@nk » Utilitaire » Convertisseur » Ajouter Comas Pour les objets numériques
URL: https://www.javascriptbank.com/add-comas-to-numeric-items.html

Ajouter Comas Pour les objets numériques © JavaScriptBank.comCet Le code JavaScript ajoute une virgule à la bonne place. Il a été conçu à l'origine pour convertir les données dans le format nnnn, nnnn., Ou nnnn.nn dans une monnaie (US) le montant, il ajoute également un signe dollar à l'avant.

Version compl�te: jsB@nk » Utilitaire » Convertisseur » Ajouter Comas Pour les objets numériques
URL: https://www.javascriptbank.com/add-comas-to-numeric-items.html



JavaScript
<SCRIPT language=JavaScript type=text/javascript><!--/* *This JavaScript takes an input number and adds commas in the proper places. *As needed by its original purpose, also adds a dollar. *Copyright 1998, David Turley <[email protected]> *Feel free to use and build on this code as long as you include this notice. *Last Modified March 3, 1998*/function commify() {    var Num = document.form.input.value;    var newNum = "";    var newNum2 = "";    var count = 0;    //check for decimal number    if (Num.indexOf('.') != -1){  //number ends with a decimal point        if (Num.indexOf('.') == Num.length-1){            Num += "00";        }        if (Num.indexOf('.') == Num.length-2){ //number ends with a single digit            Num += "0";        }        var a = Num.split(".");         Num = a[0];   //the part we will commify        var end = a[1] //the decimal place we will ignore and add back later    }    else {var end = "00";}      //this loop actually adds the commas    for (var k = Num.length-1; k >= 0; k--){      var oneChar = Num.charAt(k);      if (count == 3){        newNum += ",";        newNum += oneChar;        count = 1;        continue;      }      else {        newNum += oneChar;        count ++;      }   }  //but now the string is reversed!  //re-reverse the string  for (var k = newNum.length-1; k >= 0; k--){      var oneChar = newNum.charAt(k);      newNum2 += oneChar;  }   // add dollar sign and decimal ending from above   newNum2 = "$" + newNum2 + "." + end;   document.form.newValue.value = newNum2;}// --></SCRIPT><SCRIPT language=JavaScript type=text/javascript><!--/* *This JavaScript takes an input number and adds commas in the proper places. *As needed by its original purpose, also adds a dollar. *Copyright 1998, David Turley <[email protected]> *Feel free to use and build on this code as long as you include this notice. *Last Modified March 3, 1998*/function commify() {    var Num = document.form.input.value;    var newNum = "";    var newNum2 = "";    var count = 0;        //check for decimal number    if (Num.indexOf('.') != -1){  //number ends with a decimal point        if (Num.indexOf('.') == Num.length-1){            Num += "00";        }        if (Num.indexOf('.') == Num.length-2){ //number ends with a single digit            Num += "0";        }                var a = Num.split(".");         Num = a[0];   //the part we will commify        var end = a[1] //the decimal place we will ignore and add back later    }    else {var end = "00";}       //this loop actually adds the commas       for (var k = Num.length-1; k >= 0; k--){      var oneChar = Num.charAt(k);      if (count == 3){        newNum += ",";        newNum += oneChar;        count = 1;        continue;      }      else {        newNum += oneChar;        count ++;      }   }  //but now the string is reversed!     //re-reverse the string  for (var k = newNum.length-1; k >= 0; k--){      var oneChar = newNum.charAt(k);      newNum2 += oneChar;  }      // add dollar sign and decimal ending from above   newNum2 = "$" + newNum2 + "." + end;   document.form.newValue.value = newNum2;}// --></SCRIPT><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<FORM name=form><DIV align=center><TABLE align=center>  <TBODY>  <TR>    <TD align=right>Enter a number to convert:</TD>    <TD><INPUT name=input></TD></TR>  <TR>    <TD align=middle colSpan=2><INPUT onclick=commify() type=button value="Add Commas" name=button></TD></TR>  <TR>    <TD align=right>Here is the converted number:</TD>    <TD><INPUT name=newValue></TD></TR></TBODY></TABLE></DIV></FORM><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->