javascript的Math.round()函数为什么不能精确小数点位数,

比如:Math.round(3.4442,3);输出的值是3,而不是3.444,请问怎么能得到小数点后三位数... 比如: Math.round(3.4442,3); 输出的值是3,而不是3.444,请问怎么能得到小数点后三位数 展开
 我来答
sanshizi
2013-03-28 · TA获得超过448个赞
知道小有建树答主
回答量:364
采纳率:0%
帮助的人:307万
展开全部
round 方法, 及四舍五入

返回与给出的数值表达式最接近的整数。
Math.round(number)
必选项 number 参数是要舍入到最接近整数的值。
说明
如果 number 的小数部分大于等于 0.5,返回值是大于 number 的最小整数。否则,round 返回小于等于 number 的最大整数。

而你的写法, 根本就没有用, 没有这样的写法

你自己创建一个方法
function xround(x, num){
Math.round(x * Math.pow(10, num)) / Math.pow(10, num) ;
}

xround(3.44492, 3); // 得到 3.445
TroyLv5
推荐于2018-02-23 · TA获得超过188个赞
知道小有建树答主
回答量:215
采纳率:0%
帮助的人:108万
展开全部
我就不懂了,一个四舍五入,你们那些人何必长篇大论啊。?!楼主看我的。。
Math.round() 这个是四舍五入取整哈。

var number = 2; //或者3.4442
alert(number.toFixed(3)); //toFixed(3)代表四舍五入保留3位小数,当然也可以写2(四舍五入保留2位小数)

鄙视那些到处复制粘贴的人。。
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
音结弦无
2013-03-28 · TA获得超过535个赞
知道小有建树答主
回答量:663
采纳率:0%
帮助的人:349万
展开全部
第一种,利用math.round var original=28.453
1) //round "original" to two decimals
var
result=Math.round(original*100)/100; //returns 28.45
2) // round "original"
to 1 decimal
var result=Math.round(original*10)/10; //returns 28.5 第二种,js1.5以上可以利用toFixed(x) ,可指定数字截取小数点后 x位3) //round "original" to two decimals
var result=original.toFixed(2); //returns 28.454) // round "original" to 1 decimal
var result=original.toFixed(1); //returns 28.5 以上两种方法最通用,但却无法满足某些特殊要求,比如保留小数点后两位,如果不满两位,不满两位则补零。此时就有了第三种方法。 第三种,转换函数,这段代码来源于国外一个论坛。 [javascript]view plaincopyprint?function roundNumber(number,decimals) { var newString;// The new rounded number decimals = Number(decimals); if (decimals < 1) { newString = (Math.round(number)).toString(); } else { var numString = number.toString(); if (numString.lastIndexOf(".") == -1) {// If there is no decimal point numString += ".";// give it one at the end } var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
while (cutoff > 0 && (d1 == 9 || isNaN(d1))) { if (d1 != ".") { cutoff -= 1; d1 = Number(numString.substring(cutoff,cutoff+1)); } else { cutoff -= 1; } } } d1 += 1; } if (d1 == 10) { numString = numString.substring(0, numString.lastIndexOf(".")); var roundedNum = Number(numString) + 1; newString = roundedNum.toString() + '.'; } else { newString = numString.substring(0,cutoff) + d1.toString(); } } if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
newString += "."; } var decs = (newString.substring(newString.lastIndexOf(".")+1)).length; for(var i=0;i<decimals-decs;i++) newString += "0"; //var newNumber = Number(newString);// make it a number if you like
document.roundform.roundedfield.value = newString; // Output the result to the form field (change for your purposes)
} function roundNumber(number,decimals) {
var newString;// The new rounded number
decimals = Number(decimals);
if (decimals < 1) {
newString = (Math.round(number)).toString();
} else {
var numString = number.toString();
if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
numString += ".";// give it one at the end
}
var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
if (d1 != ".") {
cutoff -= 1;
d1 = Number(numString.substring(cutoff,cutoff+1));
} else {
cutoff -= 1;
}
}
}
d1 += 1;
}
if (d1 == 10) {
numString = numString.substring(0, numString.lastIndexOf("."));
var roundedNum = Number(numString) + 1;
newString = roundedNum.toString() + '.';
} else {
newString = numString.substring(0,cutoff) + d1.toString();
}
}
if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
newString += ".";
}
var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
for(var i=0;i<decimals-decs;i++) newString += "0";
//var newNumber = Number(newString);// make it a number if you like
document.roundform.roundedfield.value = newString; // Output the result to the form field (change for your purposes)
}

5) //round "original" to two decimals
var result=original.toFixed(2); //returns 28.456) // round "original" to 1 decimal
var result=original.toFixed(1); //returns 28.5 var original=28.4var result=original.toFixed(2); //returns 28.40
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
xinxijisu
2015-01-13 · TA获得超过361个赞
知道答主
回答量:62
采纳率:0%
帮助的人:39.3万
展开全部
最简单的做法就是如下
System.out.println(Math.round(1000*(3.4442))/1000.0);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
liujin7491
2013-03-28 · TA获得超过145个赞
知道答主
回答量:71
采纳率:0%
帮助的人:52.9万
展开全部
var a = 3.4442;
alert(a.toFixed(3));
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式