Version compl�te: jsB@nk » Form » Formatter Nombre Simple JavaScript
URL: https://www.javascriptbank.com/simple-javascript-number-formatter.html
Cet exemple de code JavaScript nous fournit deux fonctions de nettoyage et de num
Version compl�te: jsB@nk » Form » Formatter Nombre Simple JavaScript
URL: https://www.javascriptbank.com/simple-javascript-number-formatter.html
<script type="text/javascript">// Created by: Justin Barlow | http://www.netlobo.com/// This script downloaded from www.JavaScriptBank.com// This function formats numbers by adding commasfunction numberFormat(nStr){ nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) x1 = x1.replace(rgx, '$1' + ',' + '$2'); return x1 + x2;}// This function removes non-numeric charactersfunction stripNonNumeric( str ){ str += ''; var rgx = /^\d|\.|-$/; var out = ''; for( var i = 0; i < str.length; i++ ){ if( rgx.test( str.charAt(i) ) ){ if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) || ( str.charAt(i) == '-' && out.length != 0 ) ) ){ out += str.charAt(i); } } } return out;}</script>
<div>numberFormat():<br><form method="get" onsubmit="javascript:return false;"><input type="text" onkeyup="javascript:document.getElementById('numFormatResult').innerHTML = numberFormat( this.value );"><input type="reset" value="clear"></form><span id="numFormatResult"></span><br><br>stripNonNumeric():<br><form method="get" onsubmit="javascript:return false;"><input type="text" onkeyup="javascript:document.getElementById('numStripResult').innerHTML = stripNonNumeric( this.value );"><input type="reset" value="clear"></form><span id="numStripResult"></span><br><br>stripNonNumeric() then numberFormat():<br><form method="get" onsubmit="javascript:return false;"><input type="text" onkeyup="javascript:document.getElementById('numBothResult').innerHTML = numberFormat( stripNonNumeric( this.value ) );"><input type="reset" value="clear"></form><span id="numBothResult"></span></div>