代码

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. Java基础知识回顾

    Java回顾之I/O Java回顾之网络通信 Java回顾之多线程 Java回顾之多线程同步 Java回顾之集合 Java回顾之序列化 Java回顾之反射 Java回顾之一些基础概念 Java回顾之J ...

  2. [TypeScript] 建立与使用AMD Library

    [TypeScript] 建立与使用AMD Library 前言 使用Visual Studio开发TypeScript项目时,开发人员可以将可重用的程序代码,封装为AMD Library来提供其他开 ...

  3. PHP如何连接MySQL数据库

    * PHP连接MySQL数据库 * 准备工作 * 在XAMPP软件的安装目录/php/ext目录中 * php_mysql.dll和php_mysqli.dll文件必须存在 * 在XAMPP软件的安装 ...

  4. JavaScript数组的reduce方法详解

    数组经常用到的方法有push.join.indexOf.slice等等,但是有一个经常被我们忽略的方法:reduce,这个方法简直强大的不要不要的. 我们先来看看这个方法的官方概述:reduce()  ...

  5. 认识DOM和一些方法

    认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面代码 ...

  6. 关于如何在Android、Java等非微软平台上建立高信任的SharePoint应用程序

    关于如何在非微软平台上建立高信任的SharePoint应用程序 原文 :http://blogs.msdn.com/b/kaevans/archive/2014/07/14/high-trust-sh ...

  7. Android—ZXing二维码扫描遇到的问题

    最近工作中需要开发带有二维码扫描功能的软件(基于开源项目ZXing),遇到的问题记录一下,也希望给大家带来帮助. 1.首先因为扫描要开摄像机所以加权限是一定的,不然后面什么都不能进行 <uses ...

  8. Android开发学习——SQLite数据库与单元测试

    SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHelper  public class Myopenhelper extends SQLiteOpenHelp ...

  9. Intent(一.显示使用intent)

    大家都知道如果手机只有一个活动的应用,那这个应用也太简单了吧.如同网页一下,是有多个组成的,在C#中我们可以使用各程skip控件或代码,这里不再赘述.那么我们还是在当前的项目中创建一个名为Second ...

  10. 数据库服务器改名导致Reporting Service不可用的案例

    案例环境: 操作系统版本    :    Windows Server 2012 R2 Standard 数据库版本      :   SQL Server 2012 Standard Edition ...