js有没有四舍五入的函数!!

fashman 2004-08-03 08:35:39
js有没有四舍五入的函数!!
...全文
243 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ilqtj 2004-08-03
  • 打赏
  • 举报
回复
try {
(1).toFixed(1);
}
catch(e) {
Number.prototype.toFixed = function(dot) {
with(Math){
var m=pow(10,Number(dot))
var s = (round(this*m)/m).toString();
}
if(s.indexOf('.') < 0)
s += ".";
s += "00000000000000000000000000";
return s.substr(0,s.indexOf('.')+dot+1);
}
}

alert((133.996).toFixed(2));
prentice1001 2004-08-03
  • 打赏
  • 举报
回复
转贴: http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=127226


在一個輸入框中,如何在onblur事件發生前(焦點離開前),不管輸入數字有多少位小數,但Value值為四舍五入後小數點保留兩位?
---------------------------------------------------------------

<input type="text" name="textfield" onblur="javascript:this.value=parseFloat(this.value).toFixed(2)" onKeyUp="this.value=this.value.replace(/[^0-9.]/,'');">
---------------------------------------------------------------

<input type="text" name="textfield" onblur="javascript:this.value=Math.round(parseFloat(this.value)*100)/100" onKeyUp="this.value=this.value.replace(/[^0-9.]/,'');">

jaden1024 2004-08-03
  • 打赏
  • 举报
回复
Math.round(),但只能取到整数位,如果要取到第一个小数位得这样写:
Math.round(numval*10)/10

87,918

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧