四舍五入toFoxed方法
四舍五入的方法:
Number.prototype.toFixed = function (n) {
if (n > 20 || n < 0) {
throw new RangeError('toFixed() digits argument must be between 0 and 20');
}
const number = this;
if (isNaN(number) || number >= Math.pow(10, 21)) {
return number.toString();
}
if (typeof (n) == 'undefined' || n == 0) {
return (Math.round(number)).toString();
}
let result = number.toString();
const arr = result.split('.');
// 整数的情况
if (arr.length < 2) {
result += '.';
for (let i = 0; i < n; i += 1) {
result += '0';
}
return result;
}
const integer = arr[0];
const decimal = arr[1];
if (decimal.length == n) {
return result;
}
if (decimal.length < n) {
for (let i = 0; i < n - decimal.length; i += 1) {
result += '0';
}
return result;
}
result = integer + '.' + decimal.substr(0, n);
const last = decimal.substr(n, 1);
// 四舍五入,转换为整数再处理,避免浮点数精度的损失
if (parseInt(last, 10) >= 5) {
const x = Math.pow(10, n);
result = (Math.round((parseFloat(result) * x)) + 1) / x;
result = result.toFixed(n);
}
return result;
};
核心方法:
function tofixed(num,n){
let result = num.toString();
const arr = result.split('.');
const integer = arr[0];
const decimal = arr[1];
if (decimal.length == n) { //小数位等于要求长度,无需处理
return result;
}
if (decimal.length < n) {
result = integer + '.' + decimal.padEnd(2,'0');//小数位不够要求长度,补位
return result;
}
result = integer + '.' + decimal.substr(0, n);
const last = decimal.substr(n, 1);
// 四舍五入,转换为整数再处理,避免浮点数精度的损失
if (parseInt(last, 10) >= 5) {
const x = Math.pow(10, n); //10的n次方
result = (Math.round((parseFloat(result) * x)) + 1) / x;
result = result.toFixed(n);
}
return result;
}
let handleNum = tofixed(10.234,2);
console.log(handleNum); //10.23
let handleNum2 = tofixed(10.235,2);
console.log(handleNum2); //10.24
let handleNum3 = tofixed(10.5,2);
console.log(handleNum3); //10.50
四舍五入toFoxed方法的更多相关文章
- jquery中对小数进行取整、四舍五入的方法
再和大家分享一个对多位小数进行四舍五入的方法: <script language="javascript"> //对多位小数进行四舍五入 //num是要处理的数字 v为 ...
- MariaDB MySQL变量取值避免四舍五入的方法
MySQL变量取值避免四舍五入的方法 By:授客 QQ:1033553122 在一些对数据精确度要求比较高的场景(比如资金结算)下,变量取值时不能对变量值进行四舍五入操作,这时候就要做些预处理工作. ...
- SQL查询四舍五入 解决方法
方法1: SELECT CAST('123.456' as decimal) 将会得到 123(小数点后面的将会被省略掉). 如果希望得到小数点后面的两位. 则需要把上面的改为 SELECT CAST ...
- mssql sqlserver 取消数值四舍五入的方法分享
摘要: 下文讲述使用round sql函数,对数值型数据进行舍入操作 实验环境:sqlserver 2008 转自: http://www.maomao365.com/?p=6454 最近接到用户需求 ...
- c#double类型保留百分号后两位,且禁止四舍五入的方法
double percent = Convert.ToDouble(50002.3) / Convert.ToDouble(50002.5) - 0.00005; string result = pe ...
- Java几种常见的四舍五入的方法
/* * 在上面简单地介绍了银行家舍入法,目前java支持7中舍入法: 1. ROUND_UP:远离零方向舍入.向绝对值最大的方向舍入,只要舍弃位非0即进位. 2. ROUND_DOWN:趋向零方向舍 ...
- python中的向上取整向下取整以及四舍五入的方法
import math #向上取整print "math.ceil---"print "math.ceil(2.3) => ", math.ceil(2. ...
- PHP保留两位小数并且四舍五入及不四舍五入的方法
php保留两位小数并且四舍五入 $num = 123213.666666; echo sprintf("%.2f", $num); php保留两位小数并且不四舍五入 $num = ...
- Java 小数类 及四舍五入的方法 精度非常高的小数时用
注意假设结果是无限位小数,不指定位数进行四舍五入的话会报错 import java.util.Scanner; import java.math.BigDecimal; public class Ma ...
随机推荐
- 基于libuv的TCP设计(三)
基于libuv的TCP设计(一) 基于libuv的TCP设计(二) 一.第二版本的libuv_tcp已经基本可以使用.不会出错与崩溃现象,支持几百路客户端同时连接.可是有一缺陷就占用CPU非常 ...
- Appium移动自动化测试-----(四)安装 appium Server
我们可以在Appium官方网站上下载操作系统相应的Appium版本. https://bitbucket.org/appium/appium.app/downloads/ 当前最新版本为 Appium ...
- ubuntu 安装 iperf
iperf的github https://github.com/esnet/iperf/releases 解压 sudo tar -zvxf iperf-3.6.tar.gz -C /usr/loca ...
- Jackson使用
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/m0_37076574/article/d ...
- Deepin安装前分区总结
linux的分区的概念应该这么理解,为某个目录创建/挂载分区,比如为/home创建/挂载到某个分区上: 1.需要/boot分区,1.5G: 2./分区,100G:[会自动创建其他目录如/srv,/ro ...
- Word表格和文本自由互换
未完 ...... 点击访问原文(进入后根据右侧标签,快速定位到本文)
- leetcode 2019.10.29 首次破百
刷题首次破百,记录一下自己成长的历程. 仍在路上,会慢慢变强的~
- Arm-Linux 移植 mtd-utils 1.x
有关文章:<mtd-utils 的 使用> 背景: 关于在公司的生产环境有关.不希望每次都在uboot下面做nand flash 的烧写:也觉得使用U盘升级的方法比较慢,而且有关的驱动不是 ...
- Linux Nginx的权限——访问本地目录报错403
在安装好FastDFS并成功上传图片文件后,根据FastDFS返回的文件地址无法通过HTTP(即浏览器)访问到,报错404或者403. 不管是Error 404还是Error 403,基本都是Ngin ...
- Windows Mobile设备中心不能正常运行
1.开始-->运行,输入services.msc回车 2.在打开的服务界面中,找到“基于Windows Mobile 2003的连接设备” 3.打开的属性 ,找到登录项,登录身份选择“本地系统账 ...