栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

js prototype 格式化数字 By shawl.qiu

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

js prototype 格式化数字 By shawl.qiu

说明:
最近打算把 Js 练精点, 只好暂时放弃原来掌握的还行的 VBscript, 全面使用 Jscript/Javascript. 
发现 VBs 和 Js 都有些双方没有的功能...
比如 Js 就没有 VBs 的 formatNumber, formatN*** 类的函数. 
但是 Js 几乎随处可用 正则, 这是我的长处, 这点特吸引我, 不像 VBs 只有 RegExp 使用域可以使用正则.

引用一本书里的一句话:
The way to really learn a new programming language is to write programs with it.
--Javascript: The Definitive Guide, 4th Edition

目录:
1. 内容: Number.prototype.formatNumber() 源代码.
2. 效率测试

shawl.qiu 
2006-10-14
http://blog.csdn.net/btbtd

1. 内容: Number.prototype.formatNumber() 源代码.

linenum 
复制代码 代码如下:
<%  
    var $num=9876577784321.011  
        Number.prototype.formatNumber=function(pointPsti){  
          
            if(this=='')return false;  
            if(typeof(pointPsti)=='undefined'){  
                var pointPsti=3;  
            } else { if(isNaN(pointPsti)){pointPsti=3}; }  
            var num=this+'', numDc='', temp='';  
            if(num.indexOf('.')>-1){ ptPs=num.indexOf('.'); numDc=num.substr(ptPs); num=num.substr(0,ptPs); }  
            for(var i=num.length-1; i>=0;temp+=num.substr(i,1), i--);  
            var re=new RegExp('(.{'+pointPsti+'})','g');  
                temp=temp.replace(re,'$1,'); num='';                  
            for(var i=temp.length-1; i>=0; num+=temp.substr(i,1), i--);  
                num=num.replace(/^,|,$/,'')+numDc;  
            return num; // shawl.qiu script  
        }  
            Response.Write($num.formatNumber(3)+'
');  
%> 

2. 效率测试

输出 10,000 次, 耗时 2797 毫秒. 
输出 5,000 次, 耗时 1515 毫秒. 
输出 2,000 次, 耗时 672 毫秒. 
输出 1,000 次, 耗时 281 毫秒. 
输出 500 次, 耗时 140 毫秒. 
输出 100 次, 耗时 16 毫秒.
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/120260.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号