javascript中toFixed()四舍五入使用方法详解

更新时间:2023-08-08 03:58:55 阅读: 评论:0

javascript中toFixed()四舍五⼊使⽤⽅法详解
最近做的项⽬涉及到⾦额的计算,有⼀种⽅式就是进⾏四舍五⼊的规则进⾏⼩数点后⾯的尾数处理,以前⼀直以为toFixed⽅法就是四舍五⼊的,知道⼀个⽤户反馈了⾦额计算的bug我才如梦初醒(亏了⼀⽑钱),才仔细深究了下toFixed这个⽅法,唉,还是我不够严谨啊,前车之鉴,⼤家勿⾛我的⽼路!
toFixed还不同的浏览器实现,在IE10及以上⾥⾯是正常的四舍五⼊,但是别的浏览器⾥⾯就不⼀样了,它不是正常的四舍五⼊(等下重点说),⽐如:
var a = 1.335;
console.Fixed(2))
// IE  1.34
//chorme 1.33
其他的浏览器我没去⼀⼀测试,所以如果⼤家⽤了其他浏览器的还是需要去实际测试⼀下,我这⾥就说说javascript的toFixed()⽅法的四舍五⼊原理:
toFixed它是⼀个四舍六⼊五成双的诡异的⽅法(也叫银⾏家算法),"四舍六⼊五成双"含义:对于位数很多的近似数,当有效位数确定后,其后⾯多余的数字应该舍去,只保留有效数字最末⼀位,这种修约(舍⼊)规则是“四舍六⼊五成双”,也即“4舍6⼊5凑偶”这⾥“四”是指≤4 时舍去,"六"是指≥6时进上,"五"指的是根据5后⾯的数字来定,当5后有数时,舍5⼊1;当5后⽆有效数字时,需要分两种情况来讲:①5前为奇数,舍5⼊1;②5前为偶数,舍5不进。(0是偶数)
但是,经过我的测试发现,在chorme下⾯(最新版),并没有完全遵守这个规则,尤其是5的后⾯没有数字的时候,不是这么判断的,如下:
var b = 1.335
"1.33"
var b = 1.345
"1.34"
var b = 1.355
"1.35"
var b = 1.365
"1.36"
var b = 1.375
"1.38"
var b = 1.385
"1.39"
可以发现在chorme下没有完全去遵循这个规律,或许它有⾃⼰的算法,但是毕竟它没有遵循通⽤的银⾏家算法,所以tofixed 这个⽅法在涉及到⾦钱计算的业务中还是少⽤,
最好别⽤,否则可能会出⼤问题!
下⾯再再说说我⾃⼰的做法,就是根据精确位数来取⼩数点后的数,然后判断精确位是⼤于4还是⼩于等于4,上代码吧,不说了:
梦到结婚是什么预兆我们的业务是最多精确到分,也就是两位⼩数,最少就是取整,不留⼩数形容蝴蝶
function moneySwitch(money, precision){//precision是需要精确的位数,如百分位就是2
var result = 0;
//先进⾏⼀个千分位的四舍五⼊,保证3.0999这种情况在保留⼀位⼩数的时候能是对的,这⼀位可以这么做没什么问题
var money = parFloat(money).toFixed(3);
try{
var int_part = money.split(".")[0], //⼩数点前的整数
point_num = money.split(".")[1],//取⼩数点后⾯的⼩数
precision_num = point_num[3-precision];
if(precision_num>4){//五⼊的情况
if(precision==1){
point_num = parInt(point_num)+10+"";
if(point_num.length>3){//说明往整数位进1
int_part = parInt(int_part)+1+"";
point_num = point_num[1]+point_num[2];
}el{
point_num = point_num[0]+point_num[1];
}
药物英文
result = parFloat(int_part+"."+point_num);
}el if(precision==2){
point_num = parInt(point_num)+100+"";
if(point_num.length>3){//说明往整数位进1
int_part = parInt(int_part)+1+"";
point_num = point_num[1];
}el{
point_num = point_num[0];
}
result = parFloat(int_part+"."+point_num);
}el if(precision==3){
int_part = parInt(int_part)+1+"";
point_num = 0;
}
result = parFloat(int_part+"."+point_num);
}el{//四舍的情况
if(precision==1){
point_num = point_num[0]+point_num[1];
}el if(precision==2){
point_num = point_num[0];
}el if(precision==3){
point_num = 0;
}
result = parFloat(int_part+"."+point_num);
}
}catch(e){
return parFloat(money).toFixed(2);//如果过程中有出错就tofixed代替为解决 }
return result;
}
补充:
js处理数字保留2位⼩数,强制保留2位⼩数不够补上.00
1、保留两位⼩数 //功能:将浮点数四舍五⼊,取⼩数点后2位
2、//制保留2位⼩数,如:2,会在2后⾯补上00.即2.00
<!DOCTYPE html>
<html>
<head>
<meta chart="UTF-8">
<title>Test</title>情歌大全100首>狡兔三窟什么意思
<script type="text/javascript" src="js/jq.js"></script>
</head>
<script type="text/javascript">
//保留两位⼩数
/
/功能:将浮点数四舍五⼊,取⼩数点后2位
function toDecimal(x) {
var f = parFloat(x);
if (isNaN(f)) {
return;
}
f = und(x*100)/100;
return f;
}
//制保留2位⼩数,如:2,会在2后⾯补上00.即2.00
function toDecimal2(x) {
var f = parFloat(x);
if (isNaN(f)) {
return fal;
}
var f = und(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){
木型人
und(src*Math.pow(10, pos))/Math.pow(10, pos);
}
美好阳光document.write("四舍五⼊ <br/>")
document.write("3.14159267保留2位⼩数:" + toDecimal(3.14159267)+"<br/>");
document.write("3.14159267强制保留2位⼩数:" + toDecimal2(3.14159267)+"<br/>");
document.write("3.14159267保留2位⼩数:" + toDecimal(3.14559267)+"<br/>");
document.write("3.14159267强制保留2位⼩数:" + toDecimal2(3.15159267)+"<br/>");
document.write("3.14159267保留2位⼩数:" + fomatFloat(3.14559267, 2)+"<br/>");
document.write("3.14159267保留1位⼩数:" + fomatFloat(3.15159267, 1)+"<br/>");
document.write("五舍六⼊ <br/>")
document.write("1000.003保留2位⼩数:" + Fixed(2)+"<br/>");
document.write("1000.08保留1位⼩数:" + Fixed(1)+"<br/>");
document.write("1000.04保留1位⼩数:" + Fixed(1)+"<br/>");
document.write("1000.05保留1位⼩数:" + Fixed(1)+"<br/>");
document.write("科学计数 <br/>")
document.write(3.1415+"科学技术后:"+Exponential(2)+"<br/>");
document.write(3.1455+"科学技术后:"+Exponential(2)+"<br/>");
document.write(3.1445+"科学技术后:"+Exponential(2)+"<br/>");
document.write(3.1465+"科学技术后:"+Exponential(2)+"<br/>");
document.write(3.1665+"科学技术后:"+Exponential(1)+"<br/>");
document.write("精确到n位,不含n位 <br/>")
document.write("3.1415精确到⼩数点第2位" + Precision(2)+"<br/>");
document.write("3.1455精确到⼩数点第3位" + Precision(3)+"<br/>");
document.write("3.1445精确到⼩数点第2位" + Precision(2)+"<br/>");
document.write("3.1465精确到⼩数点第2位" + Precision(2)+"<br/>");
document.write("3.166592679287精确到⼩数点第5位" + Precision(5)+"<br/>"); </script>
<body>
<input type="text" id="Score" />
</body>
</html>
这篇关于toFixed()的⽂章就介绍到这了,希望⼤家以后多多⽀持。

本文发布于:2023-08-08 03:58:55,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1127864.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:没有   保留   需要   数字   计算   数点   前为
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图