解决javascript四舍五入不准确
function roundFixed(num, fixed) {
var pos = num.toString().indexOf('.'),
decimal_places = num.toString().length - pos - 1,
_int = num * Math.pow(10, decimal_places),
divisor_1 = Math.pow(10, decimal_places - fixed),
divisor_2 = Math.pow(10, fixed);
return Math.round(_int / divisor_1) / divisor_2;
}
console.log(roundFixed(3.135,2));//-> 3.14
console.log(roundFixed(2.135,2));//-> 2.14
console.log(roundFixed(0.955,1));//-> 1
console.log(roundFixed(0.955,2));//-> 0.96
console.log(roundFixed(1.955,2));//-> 1.96
console.log(roundFixed(1.955,1));//-> 2
参考:http://bbs.csdn.net/topics/391957876 perhapschen的回答
解决javascript四舍五入不准确的更多相关文章
- Javascript四舍五入(Math.round()与Math.pow())
代码 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ ...
- WebAssembly是解决JavaScript 痼疾的银弹?
写在前面 <没有银弹>是 Fred Brooks 在 1987 年所发表的一篇关于软件工程的经典论文.该论文的主要论点是,没有任何一项技术或方法可以能让软件工程的生产力在十年内提高十倍. ...
- 爬虫学习(十八)——selenium解决javascript渲染
selenium 是一个用于Web应用程序测试的工具. Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Fir ...
- 解决JavaScript中构造函数浪费内存的问题!
解决JavaScript中构造函数浪费内存的问题! 把构造函数中的公共的方法放到构造函数的原型对象上! // 构造函数的问题! function Gouzaohanshu(name, age, gen ...
- 0.1+0.2不等于0.3,微信小程序云开发如何解决JavaScript小数计算精度失准的问题
先看图 这个是JavaScript语言自身存在的一个问题.说道这里不得不提一下网上流传的JavaScript搞笑图 我们在使用云开发来开发微信小程序的时候,会经常遇到JavaScript小数计算精度失 ...
- 解决javascript动态改变img的src属性图片不显示问题
首先讲下这个bug的出现的情况,页面中有<a href="JavaScript:void(0)" onclick="document.getElementById( ...
- 解决javascript加减乘除及toFixed的误差问题
//用于替换原有的toFixed,解决精度误差问题 Number.prototype.myToFixed=function(s){ if(s == null){s = 0;} var value = ...
- javascript 四舍五入
原生 javascript 中四舍五入的函数 toFixed(n) , n为要保留的小数位数. (0<= n <=20) var num=1.0999; console.log(num.t ...
- 找出并解决 JavaScript 和 Dojo 引起的浏览器内存泄露问题
简介: 如果大量使用 JavaScript 和 Ajax 技术开发 Web 2.0 应用程序,您很有可能会遇到浏览器的内存泄漏问题.如果您有一个单页应用程序或者一个页面要处理很多 UI 操作,问题可能 ...
随机推荐
- for别名
name:for(int i =0 ;i< a.length;i++){ System.out.println(i); for(int j =0;j<i;j++){ continue na ...
- python中各种转义字符
转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \’ 单引号 \” 双引号 \a 响铃 \b 退格(Backspace) \e 转义 \000 空 \n 换行 \v 纵向制表符 \t 横向制 ...
- HDU-Digital Roots(思维+大数字符串模拟)
The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...
- rent bike问题(二分+贪心)
题目描述: A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to ...
- JDK Integer
1. public static int parseInt(String s, int radix) a. 充分考虑各种异常情况:字符串为空,带符号,进制出界,计算值出界 b. 计算时转换为负数进行处 ...
- Linux忘记roo密码的解决办法
Linux忘记root密码有三种解决办法: 下面详细介绍第一种: 重启系统后出现GRUB界面在引导装载程序菜单上,用上下方向键选择你忘记密码的那个系统键入“e” 来进入编辑模式. 接下来你可以看到 ...
- vue常见依赖安装
1):$ npm install less less-loader --save 2)style里 <style lang='less'> 2): $ npm i vue-resource ...
- java——newInstance()方法和new关键字
https://www.cnblogs.com/liuyanmin/p/5146557.html 这两个都可以创建一个对象,那么这样个东西有什么不一样呢?什么时候用new,什么时候用newInstan ...
- Hive 遇到 Class path contains multiple SLF4J bindings
Hive 遇到 Class path contains multiple SLF4J bindings Root Issue; slf4j在两处找到了jar包.分别是在Hadoop和hive的安装目录 ...
- python 基础内置函数表及简单介绍
内建函数名 (表达形式) 主要作用 备注 abs(x) 返回一个X值得绝对值(x=int/float/复数) all(iterable) 如果 iterable 的所有元素均为 True(或 iter ...