<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>floatDecimal.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
//保留两位小数
//功能:将浮点数四舍五入,取小数点后2位
function toDecimal(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return;
}
f = Math.round(x*100)/100;
return f;
}
//制保留2位小数,如:2,会在2后面补上00.即2.00 function toDecimal2(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return false;
}
var f = Math.round(x*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
} function fomatFloat(src,pos){
return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
} //四舍五入
alert("保留2位小数:" + toDecimal(3.14159267));
alert("强制保留2位小数:" + toDecimal2(3.14159267));
alert("保留2位小数:" + toDecimal(3.14559267));
alert("强制保留2位小数:" + toDecimal2(3.15159267));
alert("保留2位小数:" + fomatFloat(3.14559267, 2));
alert("保留1位小数:" + fomatFloat(3.15159267, 1));
//五舍六入
alert("保留2位小数:" + 1000.003.toFixed(2));
alert("保留1位小数:" + 1000.08.toFixed(1));
alert("保留1位小数:" + 1000.04.toFixed(1));
alert("保留1位小数:" + 1000.05.toFixed(1));
//科学计数
alert(3.1415.toExponential(2));
alert(3.1455.toExponential(2));
alert(3.1445.toExponential(2));
alert(3.1465.toExponential(2));
alert(3.1665.toExponential(1));
//精确到n位,不含n位
alert("精确到小数点第2位" + 3.1415.toPrecision(2));
alert("精确到小数点第3位" + 3.1465.toPrecision(3));
alert("精确到小数点第2位" + 3.1415.toPrecision(2));
alert("精确到小数点第2位" + 3.1455.toPrecision(2));
alert("精确到小数点第5位" + 3.141592679287.toPrecision(5));
</script>
</head>
<body>
This is my HTML page. <br>
</body>
</html>

js除法四舍五入的更多相关文章

  1. js除法四舍五入保留小数点后两位写法

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  2. js 除法 取整

    js 除法 取整 1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.roun ...

  3. JS处理四舍五入函数 toFixed(n)(可取小数点后n位)

    在JS中四舍五入的函数 toFixed(n) , n为要保留的小数位数. n为0~20,当n超过20的时候,JS会出错. 如果小数点前和要截取的前一位都是0时,不会按常理截取.       var h ...

  4. js - 除法

    取整数 1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2 ...

  5. JS的四舍五入问题

    最近踩了一个坑,mark一下toFixed四舍五入问题,详见代码: var myFixed = function(num, fix) { num = (parseFloat(num) * + ) / ...

  6. js toFixed() 四舍五入后并不是你期望的结果

    小学的时候学数学就知道有一种叫四舍五入的计算方式,就是对于小数位数的取舍,逢五进一,比如1.225 取两位小数后就是1.23.在前端开发中自己也少不了这样的计算,js也提供了相关的方法--toFixe ...

  7. js中四舍五入保留两位效数,js中将Number转换成字符类型

    今天在写代码的时候遇到了点问题,特意记下,以免忘记!四舍五入方法: // num为传入的值,n为保留的小数位 function fomatFloat(num,n){ var f = parseFloa ...

  8. JS对象 四舍五入round() round() 方法可把一个数字四舍五入为最接近的整数。 语法: Math.round(x)

    四舍五入round() round() 方法可把一个数字四舍五入为最接近的整数. 语法: Math.round(x) 参数说明: 注意: 1. 返回与 x 最接近的整数. 2. 对于 0.5,该方法将 ...

  9. js除法余数

    return (Math.round(rs*100)/100); //保保留小数点后两位数: //如果要保留三位则改为:Math.round(rs*1000)/1000; //如果要保留四位则改为:M ...

随机推荐

  1. Django过滤器

    在项目目录下建立templatetags文件 夹 建立 my_filter.py文件 from django import template register = template.Library() ...

  2. python开发遇到的坑(1)xpath解析ValueError: Unicode strings with encoding declaration are not supported

    Traceback (most recent call last): File "/Users/*******.py", line 37, in <module> Bt ...

  3. python结合pyvmomi批量关闭vmware虚拟机

    #!/usr/bin/env python #参考https://github.com/vmware/pyvmomi/blob/master/sample/poweronvm.py "&qu ...

  4. 【原创】大数据基础之Parquet(1)简介

    http://parquet.apache.org 层次结构: file -> row groups -> column chunks -> pages(data/index/dic ...

  5. asp.net MVC路由配置总结

    URL构造 命名参数规范+匿名对象 routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}&qu ...

  6. Kafka简单使用

    前言 这几天在写 shell 脚本,学到不少,但是没啥心得之类的,有空可以写个总结(但是大概率不会发表) 现在不算很忙,想再学一点 消息队列相关知识 目前比较出名的也就 RabbitMQ 和 Kafk ...

  7. nl命令

    nl (Number of Lines) 将指定的文件添加行号标注后写到标准输出.如果不指定文件或指定文件为"-" ,程序将从标准输入读取数据. 选项: -b, --body-nu ...

  8. spring Boot异步操作报错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available

    我也是最近开始学习Spring Boot,在执行异步操作的时候总是汇报如下的错误: Exception in thread "main" org.springframework.b ...

  9. Postgresql查询出换行符和回车符:

    1.有时候,业务因为回车和换行出现的错误,第一步,首先要查询出回车符和换行符那一条数据: -- 使用chr()和chr()进行查询 SELECT * )||)||'%'; -- 其实查询chr()和c ...

  10. node20180927

    1. fs读文件.写文件 // 1 fs读文件 var fs = require('fs') fs.readFile('./20180926 demo/a.text', function (err, ...