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 ...
随机推荐
- div+css背景渐变色代码示例
用CSS使DIV背景颜色渐变,适用于IE和Chrome等浏览器. 从黄到红示例:http://keleyi.com/keleyi/phtml/divcss/2.htm 代码: <style ty ...
- 【UI插件】开发一个简单日历插件(上)
前言 最近开始整理我们的单页应用框架了,虽然可能比不上MVVM模式的开发效率,也可能没有Backbone框架模块清晰,但是好歹也是自己开发出来 而且也用于了这么多频道的东西,如果没有总结,没有整理,没 ...
- flex总结
容器 属性: 属性的值: 项目 属性: 属性的值:
- String类型的属性和方法
× 目录 [1]属性 [2]对象通用方法 [3]访问字符方法[4]字符串拼接[5]创建子串方法[6]大小写转换[7]查找子串位置[8]正则匹配方法[9]去除首尾空格[10]字符串比较 前面的话 前面已 ...
- js的闭包概念
一.变量的作用域要懂得闭包,起首必须懂得Javascript特别的变量作用域.变量的作用域无非就是两种:全局变量和局部变量.Javascript说话的特别之处,就在于函数内部可以直接读取全局变量. J ...
- 用UILocalNotification实现一个闹钟(Swift)
之前项目需求要实现一个闹钟,github上找了半天发现都是很旧的代码了,所以就准备自己写一个,刚好最近在学习Swift,就用Swift写了一个demo放在这里:https://github.com/P ...
- iOS多线程之1.从Thread看多线程的生命周期
Thread 是多线程中最容易理解,但是使用起来又是最麻烦的一种多线程方法.为什么说容易理解呢?一个NSThread的对象就是一条线程.使用起来麻烦是因为,需要我们自己管理线程的生命周期:创建线程 ...
- HTML5快速入门(三)—— 标签综合运用
前言: 1.HTML5的发展非常迅速,可以说已经是前端开发人员的标配,在电商类型的APP中更是运用广泛,这个系列的文章是本人自己整理,尽量将开发中不常用到的剔除,将经常使用的拿出来,使需要的朋友能够真 ...
- [Unity游戏开发]向量在游戏开发中的应用(三)
本文已同步发表在CSDN:http://blog.csdn.net/wenxin2011/article/details/51088236 在上一篇博客中讲了利用向量点乘在游戏开发中应用的几种情景.本 ...
- [windows]win10家庭版切换到管理员账户
背景:很多时候,在安装或者运行某些程序时会需要到管理员账户运行.而在win10家庭版却没有明显的位置可以让用户简单的进行切换.因此,有了以下的方法. 方法: 1.在搜索框中输入CMD,右键以管理员方式 ...