Javascript四舍五入(Math.round()与Math.pow())
代码
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Javascript四舍五入(Math.round()与Math.pow())</title>
<script type="text/javascript">
//Math.round(x);返回数字最接近的整数,四舍五入取整数,即舍去小数部分
function f(){
alert(Math.round(123.567));
alert(Math.round(123.456));
}
//Math.pow(x,y);返回底数的指定次幂
//返回以x的y次幂,等同于x的y次幂的数值表达式
//如果pow的参数过大而引起浮点溢出,返回Infinity
function f1(){
alert(Math.pow(2,10));//2的10次方等于1024
alert(Math.pow(1024,0.1));//1024的0.1次方等于2
alert(Math.pow(99,9999));//溢出则返回Infinity
}
/*Javascript设置要保留的小数位数,四舍五入。
*ForDight(Dight,How):数值格式化函数,Dight要格式化的 数字,How要保留的小数位数。
*这里的方法是先乘以10的倍数,然后去掉小数,最后再除以10的倍数。
*/
function ForDight(Dight,How){
Dight = Math.round(Dight*Math.pow(10,How))/Math.pow(10,How);
return Dight;
}
function f2(){
alert(ForDight(12345.67890,3));//保留三位小数
alert(ForDight(123.99999,4));//保留四位小数
}
//另外一种四舍五入的方法,原理一样。
//里面的两个参数:num就是要转换的数据。n为要转换的位数
//cheng(123.456,2);//保留两位小数
function cheng(num,n){
var dd=1;
var tempnum;
for(i=0;i<n;i++){
dd*=10;
}
tempnum = num*dd;
tempnum = Math.round(tempnum);
alert(tempnum/dd);
}
</script>
</head>
<body>
<input type="button" value="round" onclick="f();" />
<input type="button" value="pow" onclick="f1();" />
<input type="button" value="设置要保留的小数位数,四舍五入" onclick="f2();" />
<input type="button" value="cheng" onclick="cheng(123.456,2);" />
</body>
</html>
代码
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><script type="text/javascript">
//用Javascript取float型小数点后两位,例22.127456取成22.13,如何做?
//1.最笨的办法....... [我就怎么干的.........]
function get(){
var s = 22.127456 + "";
var str = s.substring(0,s.indexOf(".") + 3);
alert(str);
}
</script>
<script type="text/javascript">
//2. 正则表达式效果不错
onload = function(){
var a = "23.456322";
var aNew;
var re = /([0-9]+\.[0-9]{2})[0-9]*/;
aNew = a.replace(re,"$1");
alert(aNew);
}
</script>
<script type="text/javascript">
//3. 他就比较聪明了.....
var num=22.127456;
alert( Math.round(num*100)/100);
</script>
<script type="text/javascript">
//4.会用新鲜东西的朋友....... 但是需要 IE5.5+才支持。
var num=22.127456;
alert( num.toFixed(2));
</script>
文章来自:http://my.oschina.net/haquanwen/blog/144033?p=1
Javascript四舍五入(Math.round()与Math.pow())的更多相关文章
- JavaScipt中的Math.ceil() 、Math.floor() 、Math.round()、Math.pow() 三个函数的理解
以前一直会三个函数的使用产生混淆,现在通过对三个函数的原型定义的理解,其实很容易记住三个函数. 现在做一个总结: 1. Math.ceil()用作向上取整. 2. Math.floor()用作向下取整 ...
- Javascript Math.ceil()与Math.round()与Math.floor()区别
Math.ceil()向上舍入 1 2 3 alert(Math.ceil(20.1)) //输出 21 alert(Math.ceil(20.5)) //输出 21 alert(Math.ceil( ...
- Javascript -- Math.round()、Math.ceil()、Math.floor()、parseInt去小数取整总结
一.Math.round() 作用:四舍五入返回整数.(返回参数+0.5后,向下取整) Math.round(5.57) //返回6 Math.round(2.4) //返回2 Math.round( ...
- JavaScript中的Math.ceil()、Math.round()、Math.floor()
1. Math.ceil():向上取整(指取大于该浮点数的最小整数) 2. Math.round():四舍五入取整(注意:当该浮点数距离两端整数一样时,取较大的那个整数,如Math.round(-1. ...
- Math.round()、Math.ceil()、Math.floor()与Math.random()的区别?
Math.round(x) 四舍五入 加上0.5向下取整 Math.round(1.5) 2 Math.round(-11.5) -11 Math.round(-11.2) -10 Math.ceil ...
- Math.round、Math.floor、Math.ceil 区别
1.Math.round() 按照四舍五入的方式返回值 例如:Math.round(9.5)=10 Math.round(9.4)=9 2.Math.floor()返回最小整数 例如:Math. ...
- Math.round(),Math.ceil(),Math.floor()的区别
1.Math.round():根据"round"的字面意思"附近.周围",可以猜测该函数是求一个附近的整数,看下面几个例子就明白. 小数点后第一位<5 正 ...
- callable函数 stride的意义 Math.round(),Math.ceil(),Math.floor()用法
callable()函数检查一个函数是否可以调用 如果返回True,object仍然可能调用失败:但如果返回False,调用对象ojbect绝对不会成功. 对于函数, 方法, lambda 函式, 类 ...
- Math.round(),Math.ceil(),Math.floor()
Math.round() :round周围,求一个附近的 整数 小数点后第一位 < 5 正数:Math.round(10.48) // 10 负数:Math.round(-10.4 ...
随机推荐
- Hibernate(十)__缓存机制
为什么需要缓存? 缓存的作用主要用来提高性能,可以简单的理解成一个Map: 使 用缓存涉及到三个操作:把数据放入缓存.从缓存中获取数据. 删除缓存中的无效数据. 从上图看出: 当我们去查询对象的时候, ...
- 安全退出,清空Session或Cookie
概览: 网站中点击退出,如果仅仅是重定向到登录/出页面,此时在浏览器地址栏中输入登录后的某个页面地址如主页,你会发现不用登录就能访问.这种所谓的退出并不是安全的. 那么怎样做到安全退出呢? 那就是点击 ...
- EasyUI弹出窗口实例
效果体验:http://hovertree.com/texiao/jeasyui/1.htm 源代码下载:HovertreeJEasyUI HTML文件代码: <!DOCTYPE html> ...
- angular源码分析:angular中$rootscope的实现——scope的一生
在angular中,$scope是一个关键的服务,可以被注入到controller中,注入其他服务却只能是$rootscope.scope是一个概念,是一个类,而$rootscope和被注入到cont ...
- 函数模块:CTVB_COMPARE_TABLES--两个表中删除/变更/粘贴分解
这个函数模块比较两个内表,将被删除.增加和修改的内表行分别分组输出. 输入参数:TABLE_OLD:旧表TABLE_NEW:新表KEY_LENGTH:键长度,指定内表中的前若干个字节(在 Unicod ...
- Mou常用快捷键
title: Mou常用快捷键date: 2015-11-08 17:16:38categories: 编辑工具 tags: mou 小小程序猿我的博客:http://daycoding.com Vi ...
- CSS3选择器——基本选择器
CSS是一种用于屏幕上渲染html,xml等一种语言,CSS主要是在相应的元素中应用样式,来渲染相对应用的元素,那么这样我们选择相应的元素就很重要了,如何选择对应的元素,此时就需要我们所说的选择器.选 ...
- 在 CentOS7 上安装 Tomcat9
在 CentOS7 上安装 Tomcat9 1 通过 SecureCRT 连接到阿里云 CentOS7 服务器: 2 进入到目录 /usr/local/ 中: cd /usr/local/ 3 创建目 ...
- Redis 支持的5种数据结构
redis的崛起绝非偶然,它确实有自己的新东西在里面,它不像Memcached,只能将数据存储在内存中,它提供了持久化机制和数据同步,避免了宕机后的雪崩的问题,即服务器出现问题后,内存中保留的原始数据 ...
- 软件工程--界面UI 的原型设计
经过今天的讨论, 我们组初步给出了设计方案. 安装了一晚上的 mockupBuilder 终于绘制了几份界面的原型图, 这里需要吐槽下 mockupBuilder, 这个软件很好用, 不过网页版和单机 ...