如何在JavaScript中四舍五入一个数字?
Math.round()不起作用,因为它舍入到最接近的小数。
我不确定是否有更好的方法,而不是在小数点处分开,保留第一位。一定有……
如何在JavaScript中四舍五入一个数字?
Math.round()不起作用,因为它舍入到最接近的小数。
我不确定是否有更好的方法,而不是在小数点处分开,保留第一位。一定有……
当前回答
今天摆弄别人的代码,发现下面的似乎也round down:
var dec = 12.3453465,
int = dec >> 0; // returns 12
有关符号传播右移(>>)的更多信息,请参阅MDN位操作符
我花了一段时间才弄清楚这是在做什么:D
但正如上面强调的那样,Math.floor()在我看来更易于阅读。
其他回答
使用Math.floor()是做到这一点的一种方法。
更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
如果需要四舍五入到特定的小数点后数位,可以尝试使用此函数
function roundDown(number, decimals) {
decimals = decimals || 0;
return ( Math.floor( number * Math.pow(10, decimals) ) / Math.pow(10, decimals) );
}
例子
alert(roundDown(999.999999)); // 999
alert(roundDown(999.999999, 3)); // 999.999
alert(roundDown(999.999999, -1)); // 990
Math.round(3.14159 * 100) / 100 // 3.14
3.14159.toFixed(2); // 3.14 returns a string
parseFloat(3.14159.toFixed(2)); // 3.14 returns a number
Math.round(3.14159) // 3
Math.round(3.5) // 4
Math.floor(3.8) // 3
Math.ceil(3.2) // 4
要四舍五入到负无穷,可以使用:
rounded=Math.floor(number);
要四舍五入到零(如果数字可以四舍五入到-2147483648和2147483647之间的32位整数),使用:
rounded=number|0;
要四舍五入到零(对于任何数字),使用:
if(number>0)rounded=Math.floor(number);else rounded=Math.ceil(number);
这是数学。在一个简单的例子中使用地板。这可能会帮助新开发人员了解如何在函数中使用它以及它的功能。希望能有所帮助!
<script>
var marks = 0;
function getRandomNumbers(){ // generate a random number between 1 & 10
var number = Math.floor((Math.random() * 10) + 1);
return number;
}
function getNew(){
/*
This function can create a new problem by generating two random numbers. When the page is loading as the first time, this function is executed with the onload event and the onclick event of "new" button.
*/
document.getElementById("ans").focus();
var num1 = getRandomNumbers();
var num2 = getRandomNumbers();
document.getElementById("num1").value = num1;
document.getElementById("num2").value = num2;
document.getElementById("ans").value ="";
document.getElementById("resultBox").style.backgroundColor = "maroon"
document.getElementById("resultBox").innerHTML = "***"
}
function checkAns(){
/*
After entering the answer, the entered answer will be compared with the correct answer.
If the answer is correct, the text of the result box should be "Correct" with a green background and 10 marks should be added to the total marks.
If the answer is incorrect, the text of the result box should be "Incorrect" with a red background and 3 marks should be deducted from the total.
The updated total marks should be always displayed at the total marks box.
*/
var num1 = eval(document.getElementById("num1").value);
var num2 = eval(document.getElementById("num2").value);
var answer = eval(document.getElementById("ans").value);
if(answer==(num1+num2)){
marks = marks + 10;
document.getElementById("resultBox").innerHTML = "Correct";
document.getElementById("resultBox").style.backgroundColor = "green";
document.getElementById("totalMarks").innerHTML= "Total marks : " + marks;
}
else{
marks = marks - 3;
document.getElementById("resultBox").innerHTML = "Wrong";
document.getElementById("resultBox").style.backgroundColor = "red";
document.getElementById("totalMarks").innerHTML = "Total Marks: " + marks ;
}
}
</script>
</head>
<body onLoad="getNew()">
<div class="container">
<h1>Let's add numbers</h1>
<div class="sum">
<input id="num1" type="text" readonly> + <input id="num2" type="text" readonly>
</div>
<h2>Enter the answer below and click 'Check'</h2>
<div class="answer">
<input id="ans" type="text" value="">
</div>
<input id="btnchk" onClick="checkAns()" type="button" value="Check" >
<div id="resultBox">***</div>
<input id="btnnew" onClick="getNew()" type="button" value="New">
<div id="totalMarks">Total marks : 0</div>
</div>
</body>
</html>