正确的显示的是:87590.09但是运算出来的确是:87590.090000…","url":"https://www.cnblogs.com/ipusr/archive/2011/02/28/1966704.html","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.cnblogs.com/ipusr/archive/2011/02/28/1966704.html"},"image":"https://juhe.plus/apple-touch-icon.png","publisher":{"@type":"Organization","name":"惯性聚合","logo":{"@type":"ImageObject","url":"https://juhe.plus/apple-touch-icon.png"}},"author":{"@type":"Person","name":"ipusr"},"datePublished":"2011-02-28T01:42:00.000Z"}]}
惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
Recorded Future
Recorded Future
Jina AI
Jina AI
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
L
LangChain Blog
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
博客园 - 【当耐特】
雷峰网
雷峰网
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
有赞技术团队
有赞技术团队
L
Lohrmann on Cybersecurity
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
P
Privacy & Cybersecurity Law Blog
Scott Helme
Scott Helme
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Hacker News - Newest:
Hacker News - Newest: "LLM"
NISL@THU
NISL@THU
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog RSS Feed
Cyberwarzone
Cyberwarzone
K
Kaspersky official blog
F
Full Disclosure
Martin Fowler
Martin Fowler
Spread Privacy
Spread Privacy
D
Docker
C
Cisco Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page

博客园 - 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+"";    

};