<div class="codetitle"> <span><a style="CURSOR: pointer" data="58318" class="copybut" id="copybut58318" onclick="doCopy('code58318')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code58318"> <br><script type="text/javascript"><BR>var format = function (number, form) {<BR> var forms = form.split('.'), number = '' + number, numbers = number.split('.')<BR> , leftnumber = numbers[0].split('')<BR> , exec = function (lastMatch) {<BR> if (lastMatch == '0' || lastMatch == '#') {<BR> if (leftnumber.length) {<BR> return leftnumber.pop();<BR> } else if (lastMatch == '0') {<BR> return lastMatch;<BR> } else {<BR> return '';<BR> }<BR> } else {<BR> return lastMatch;<BR> }<BR> }, string<br><br> string = forms[0].split('').reverse().join('').replace(/./g, exec).split('').reverse().join('');<BR> string = leftnumber.join('') + string;<br><br> if (forms[1] && forms[1].length) {<BR> leftnumber = (numbers[1] && numbers[1].length) ? numbers[1].split('').reverse() : [];<BR> string += '.' + forms[1].replace(/./g, exec);<BR> }<BR> return string.replace(//.$/, '');<BR>};<BR>alert(format(123, '#.00')) //输出 123.00 <BR>alert(format(123, '0.##')) //输出 123 <BR>alert(format(123, '0000.00 ')) //输出 0123.00 <BR>alert(format(123456, '###,###.00')) //输出 123,456.00 <BR></script><br> </div>