惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - ipusr

SQL 压缩日志 SQL性能优化总结 - ipusr - 博客园 net dll 破解 Balsamiq Mockups 小窍门 Web键盘操作键值表 - ipusr - 博客园 .NET 项目中用到的广告效果 如何防止空链接 # 返回到页面 - ipusr PowerDesigner使用注意事项 sql表数据生成insert HBKrnl木马病毒杀毒办法(HBKernel.sys) (转)jQuery 基础 - ipusr - 博客园 使用Javascript操作DOM的一些方法--元素的访问/复制等 - ipusr - 博客园 JS获取所有对象 javascript - ipusr - 博客园 用户中心 - 博客园 数字验证 控件 - ipusr - 博客园 Net读取Execl问题回顾 - ipusr - 博客园 SET NOCOUNT RowCount 説明 sysloader 木马清除方法
javascript对float数值进行运算的误差解決
ipusr · 2011-02-28 · via 博客园 - ipusr

<script language="javascript">   

alert(87593.21-3.12);   

</script>  

正确的显示的是:87590.09
但是运算出来的确是:87590.09000000001

可采用 toFixed处理,如:

<script language="javascript">   

alert((87593.21-3.12).toFixed(2));   

</script>  

---------------------------------

toFixed 方法问题:

<script language="javascript">   

alert(0.009.toFixed(2));   

</script>  

运算的结果0.00,但是需要的是 0.01

解决方法:

Number.prototype.toFixed = function(d)    

{    

    var s=this+"";if(!d)d=0;    

    if(s.indexOf(".")==-1)s+=".";s+=new Array(d+1).join("0");    

    if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+ (d+1) +"})?)\\d*$").test(s))    

   {    

       var s="0"+ RegExp.$2, pm=RegExp.$1, a=RegExp.$3.length, b=true;    

      if (a==d+2){a=s.match(/\d/g); if (parseInt(a[a.length-1])>4)    

      {    

           for(var i=a.length-2; i>=0; i--) {a[i] = parseInt(a[i])+1;    

            if(a[i]==10){a[i]=0; b=i!=1;} else break;}    

        }    

        s=a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2");    

    }if(b)s=s.substr(1);return (pm+s).replace(/\.$/, "");} return this+"";    

};