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 ...
随机推荐
- 关于arcgis engine的注记显示与关闭问题
1.注记的添加需要拿到IGeoFeatureLayer接口下的AnnotationProperties属性,转为IAnnotationLayerPropertiesCollection接口,并创建一个 ...
- sharepoint 提升权限报错
现象: sharepoint中提升权限是为了模拟管理员操作,但是对于普通用户对item的更新和删除照成错误 解决办法: 去掉权限升级 说明: 升级权限能不用尽量不用,同时也不好排查权限问题.
- SharePoint 2010 GridView/SPGridView完全应用系统样式
自定义开发页面如果用到了GridView或SPGridView默认跟列表的样式是不一样的,如要要一样,需要: 1)aspx <asp:GridView DataKeyNames="ID ...
- 初识android中的动画
动画效果可以大大提高界面的交互效果,因此,动画在移动开发中的应用场景较为普遍.掌握基本的动画效果在成熟的软件开发中不可或缺.除此之外,用户对于动画的接受程度远高于文字和图片,利用动画效果可以加深用户对 ...
- OpenGL FrameBufferCopy相关Api比较(glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D)
OpenGL FrameBufferCopy相关Api比较 glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D 标题所述 ...
- ios 提取html 字符串中的img 的地址(图片地址)
本文原文地址 http://www.cnblogs.com/qianLL/p/6082287.html 有时候 后台返回的是一串html'字符串 我们需要把里面的图片地址提取出来 这个关键就是一个正 ...
- iOS 系统根据导航栏和状态栏自动修改布局
问题 条件:1.有一个全屏大小的带导航的controller 2.隐藏导航栏,最顶上还会留出状态栏的位置,而不是全屏显示 解决方法 self.automaticallyAdjustsScrollVie ...
- SE(homework3)_敏捷模型
今天老师上课主要和我们讲解了软件开发模型类型.既然是敏捷模型,那么什么是非敏捷模型呢?了解这里点,会更清楚什么是敏捷模想.我们所知道的非敏捷模型有瀑布模型,我们知道这是早期软件开发的经典模型,流程主要 ...
- XML语言基础2 DTD
XML DTD 文档类型定义(DTD)可定义合法的XML文档构建模块.它使用一系列合法的元素来定义文档结构. DTD可被声明于XML文档中,也可以作为一个外部的引用. 内部的DOCTYPE声明 假如D ...
- SSIS 2010 BUG 一例
Sample data :abc|"edfg|xyz Test 1: Text Qualified is set to nothing. the result is in good for ...