/ Published in: JavaScript
URL: http://www.anieto2k.com/2007/03/09/10-utilidades-javascript-que-deberias-conocer/
Format a number adding thousands separator and unformat the result of the formatting. The number can be rounded up the specified decimal places.
It uses ',' as decimal separator and '.' as thousand separator.
Expand |
Embed | Plain Text
//Deshace el formato creado por fmtNum String.desFmtNum = function(num) { return num.replace(/([^0-9,\-])/g,'').replace(/,/g,'.')*1; }; //Formatea un número con '.' como separador de miles y ',' como separador decimal. Number.fmtNum = function(num,numDecs){ if (typeof(num) == 'string') num = desFmtNum(num); if (numDecs) num = num.toFixed(numDecs); num += ''; var splitStr = num.split('.'); var splitLeft = splitStr[0]; var splitRight = splitStr.length > 1 ? ',' + splitStr[1] : ''; var regx = /(\d+)(\d{3})/; while (regx.test(splitLeft)) { splitLeft = splitLeft.replace(regx, '$1' + '.' + '$2'); } if (numDecs>0) return splitLeft + splitRight; else return splitLeft; };
You need to login to post a comment.
