代码

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())的更多相关文章

  1. JavaScipt中的Math.ceil() 、Math.floor() 、Math.round()、Math.pow() 三个函数的理解

    以前一直会三个函数的使用产生混淆,现在通过对三个函数的原型定义的理解,其实很容易记住三个函数. 现在做一个总结: 1. Math.ceil()用作向上取整. 2. Math.floor()用作向下取整 ...

  2. 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( ...

  3. Javascript -- Math.round()、Math.ceil()、Math.floor()、parseInt去小数取整总结

    一.Math.round() 作用:四舍五入返回整数.(返回参数+0.5后,向下取整) Math.round(5.57) //返回6 Math.round(2.4) //返回2 Math.round( ...

  4. JavaScript中的Math.ceil()、Math.round()、Math.floor()

    1. Math.ceil():向上取整(指取大于该浮点数的最小整数) 2. Math.round():四舍五入取整(注意:当该浮点数距离两端整数一样时,取较大的那个整数,如Math.round(-1. ...

  5. 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 ...

  6. Math.round、Math.floor、Math.ceil 区别

    1.Math.round() 按照四舍五入的方式返回值 例如:Math.round(9.5)=10    Math.round(9.4)=9 2.Math.floor()返回最小整数 例如:Math. ...

  7. Math.round(),Math.ceil(),Math.floor()的区别

    1.Math.round():根据"round"的字面意思"附近.周围",可以猜测该函数是求一个附近的整数,看下面几个例子就明白. 小数点后第一位<5 正 ...

  8. callable函数 stride的意义 Math.round(),Math.ceil(),Math.floor()用法

    callable()函数检查一个函数是否可以调用 如果返回True,object仍然可能调用失败:但如果返回False,调用对象ojbect绝对不会成功. 对于函数, 方法, lambda 函式, 类 ...

  9. Math.round(),Math.ceil(),Math.floor()

    Math.round() :round周围,求一个附近的 整数 小数点后第一位 < 5 正数:Math.round(10.48)       //  10 负数:Math.round(-10.4 ...

随机推荐

  1. zookeeper集群

    0,Zookeeper基本原理 ZooKeeper集群由一组Server节点组成,这一组Server节点中存在一个角色为Leader的节点,其他节点都为Follower.当客户端Client连接到Zo ...

  2. 【HTML5&CSS3进阶学习01】气泡组件的实现

    前言 气泡组件在实际工作中非常普遍,无论是网页中还是app中,比如: 我们这里所谓气泡组件是指列表型气泡组件,这里就其dom实现,css实现,js实现做一个讨论,最后对一些细节点做一些说明,希望对各位 ...

  3. Dynamics XRM Tools 2015 2016

    Download Link: Dynamics XRM Tools 2015/2016 Overview Dynamics XRM Tools brings you a quality range o ...

  4. How To Collect ULS Log from SharePoint Farm

    We can use below command to collect SharePoint ULS log from all servers in the Farm in PowerShell. M ...

  5. 浅谈Bluetooth蓝牙开发

    前言:项目用到蓝牙开发,具体蓝牙获取硬件传感器中的数据. 因为没有蓝牙开发的相关经验,决定先了解一些蓝牙开发的知识,再去看之前同事写的蓝牙相关代码. ------------------------- ...

  6. LinearLayout布局问题

    LinearLayout是平时开发中很常见的线性布局方式,分为水平和竖直2种,笔者在实际使用中发现了如下问题,希望能帮到别人. 横着的LinearLayout,凡是设置x坐标的属性都不起作用,比如la ...

  7. php关键词替换的类(避免重复替换,保留与还原原始链接)

    转载:http://www.169it.com/blog_article/601549531.html 本节主要内容:一个关键词替换的类 主要可以用于关键词过滤,或关键词查找替换方面. 实现过程分析: ...

  8. 记录一次Quartz2D学习(一)

    经常看点 drawRect的重写  但是不知道这究竟是神马 今天开始学习这一块的东西,更确切地说是深入 早在view的时候 就经常会调用layer的maskToBounds属性,其实 重写 drawR ...

  9. [css]我要用css画幅画(六)

    接着之前的[css]我要用css画幅画(五), 由于这个画已经画了很久了,这次一次性加了比较多更新,算是让这幅画告一段落吧. 本次的更新包括: 1. 给云增加动画 2. 画了一棵树 3. 树上画了一个 ...

  10. RMAN备份脚本一列分享

    在ORACLE数据库中,RMAN备份的脚本非常多,下面介绍一例shell脚本如何通过RMAN备份,以及FTP上传RMAN备份文件以及归档日志文件的脚本. fullback.sh 里面调用RMAN命令做 ...