javascript NUMBER toFixed()函数
来源:互联网 发布:什么是无线传感器网络 编辑:程序博客网 时间:2024/05/22 05:13
关于toFixed用途,及bug我就不赘述了,请看
http://www.cnblogs.com/jaxu/archive/2013/07/15/3191187.html
下面是我的修改方案
Number.prototype.toFixed=function(len){
var add = 0;
var s,temp;
var s1 = this + "";
var start = s1.indexOf(".");
if(start!=-1 && s1.substr(start+len+1,1) == "") return this/1; //如果去掉这行 可以在控制台试试530.30.toFixed(2) 结果很惊喜哦
if(start!=-1 && s1.substr(start+len+1,1)>=5) add=1;
var temp = Math.pow(10,len);
s = Math.floor(this * temp) + add;
return s/temp;
}
有高手可以给我解释解释 不添加if(start!=-1 && s1.substr(start+len+1,1) == "") return this/1; 这行,为啥子530.30.toFixed(2) 怎么就变成了530.29呢?
都是醉了
没有考虑负数情况
Number.prototype.toFixed=function(len){
var add = 0;
var s,temp;
var s1 = this + "";
var start = s1.indexOf(".");
if(start!=-1 && s1.substr(start+len+1,1) == "") return this/1;
if(start!=-1 && s1.substr(start+len+1,1)>=5) add=1;
var temp = Math.pow(10,len);
if (this > 0) {
s = Math.floor(this * temp) + add;
}else{
s = Math.ceil(this * temp) - add;
}
return s/temp;
}
- javascript NUMBER toFixed()函数
- Number.toFixed() 函数详解
- JavaScript toFixed() parseFloat() 函数
- JavaScript toFixed(num) 函数
- Javascript -- toFixed()函数
- <javascript>Number.prototype.toFixed小记
- JS四舍五入函数-- toFixed(number)
- JavaScript parseInt() &parseFloat & toFixed函数
- JavaScript Number对象的toFixed() 方法
- js number() ,parseFloat(), toFixed(), toPrecision() ,round()函数
- 【JS】函数 Number()、parseInt()、parseFloat()以及Number.toFixed(x) 、 Number.toPrecision(x) 、 Math.Round(x)
- JavaScript编程-JS保留两位小数 四舍五入函数toFixed()
- JavaScript toFixed() 方法
- JavaScript toFixed() 方法
- JavaScript toFixed() 方法
- JavaScript小数四舍五入toFixed
- JavaScript toFixed() 方法
- JavaScript toFixed() 方法
- 数据库原理第一章作业
- 【原创】PHP 7内核之HashTable实现
- UIImage imageOrientation问题
- Sicily 1876/1949. Basic Graph Problem
- Android 带密码输入界面的Dialog实现机制
- javascript NUMBER toFixed()函数
- 新博客开通
- 实现不将最近使用的Word文件添加到Word本身的最近使用的文件列表中
- php批量导入ecxel用户名(拼音、ip)
- Sicily 1829/1948. Largest Area
- Opencv&C++获取摄像头
- MediaPlayer状态机
- Sicily 1827/1947. Snipers
- 继承于多态