function formatNum(num) //将数字转换成三位逗号分隔的样式
{
  if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)){alert("wrong!"); return num;}
  var a = RegExp.$1, b = RegExp.$2, c = RegExp.$3;
  var re = new RegExp().compile("(\\d)(\\d{3})(,|$)");
  while(re.test(b)) b = b.replace(re, "$1,$2$3");
  return a +""+ b +""+ c;
}
alert(formatNum(-12345678.123));
alert(formatNum("12345678.123"));
alert(formatNum("10000000000000000000000000000000000000000"));

解决方案 »

  1.   


    alert(formatNum("1.00000000000000000000000000000000000000001"));
      

  2.   

    请问javascript如何把30000000.1286转换成30,000,000.13
      

  3.   

    function formatNum(num, digit) //将数字转换成三位逗号分隔的样式
    {
      if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)){alert("wrong!"); return num;}
      var a = RegExp.$1, b = RegExp.$2, c = RegExp.$3;
      var re = new RegExp().compile("(\\d)(\\d{3})(,|$)");
      while(re.test(b)) b = b.replace(re, "$1,$2$3");
      if (c && digit && new RegExp("^.(\\d{"+ digit +"})(\\d)").test(c)){
      if (RegExp.$2>4) c = (parseFloat(RegExp.$1)+1)/Math.pow(10, digit);
      else c = "."+ RegExp.$1;}
      return a +""+ b +""+ (c+"").substr((c+"").indexOf("."));
    }
    alert(formatNum(-12345678.005, 2));
    alert(formatNum("12345678.1256246", 6));
    alert(formatNum("10000000000000000000000000000000000000000"));
      

  4.   

    <script language="vbscript">
    function formatNum (str, n)
      formatNum = FormatNumber(str,n)
    end function
    </script>
    <script>
    alert(formatNum("12345678.1256246", 2))
    </script>
      

  5.   

    function formatNum(num, digit) //将数字转换成三位逗号分隔的样式
    {
    if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)){alert("wrong!"); return num;}
    var a = RegExp.$1, b = RegExp.$2, c = RegExp.$3;
    var re = new RegExp().compile("(\\d)(\\d{3})(,|$)");
    while(re.test(b)) b = b.replace(re, "$1,$2$3");
    if (c && digit && new RegExp("^.(\\d{"+ digit +"})(\\d)").test(c)){
    if (RegExp.$2>4) c = (parseFloat(RegExp.$1)+1)/Math.pow(10, digit);
    else c = "."+ RegExp.$1;}
    return a +""+ b +""+ (c+"").substr((c+"").indexOf("."));
    }
    //alert(formatNum(-12345678.005, 2));
    //alert(formatNum("12345678.1256246", 6));
    //alert(formatNum("10000000000000000000000000000000000000000"));

    var mm=1;
    function imgzoom(h)
    {   
    if (h==1)
    {
    mm+=0.2;
    mm=formatNum(Math.min(2,mm),2);
    document.all.imgjpg.style.zoom=mm;
    window.htm_all.style.zoom=mm;
    alert(mm*100+"%");
    }
    else if(h==2)
    {
    mm-=0.2;
    mm =formatNum(Math.max(0.05, mm),2);
    document.all.imgjpg.style.zoom=mm;
    window.htm_all.style.zoom=mm;
    alert(mm*100+"%");
    //document.body.style.zoom=mm;
    }
    }
    可是这样会报错
      

  6.   

    http://community.csdn.net/Expert/topic/3880/3880020.xml?temp=.8474542
    关注这个问题。对了。楼上的哥们,是JAVA脚本