JavaScript舍入不起作用

JavaScript Rounding not working

本文关键字:不起作用 舍入 JavaScript      更新时间:2023-09-26

我正在尝试创建javascript代码,将数字四舍五入到最接近的整数、十分之一、百分之一和千分之一位,但它不起作用。你知道为什么吗?

<!DOCTYPE html>
<html>
<title>Rounding</title>
<head>
<script  type="text/javascript">
    var roundToInteger(x);
    var roundToTenths(x);
    var roundToHundredths(x);
    var roundToThousandths(x);
    x = window.prompt("Enter a integer");
    function roundToInteger()
    {
    x = Math.round(x);
    }
    function roundToTenths()
    {
    x = Math.round(x * 10 + .5) / 10;
    }
    function roundToHundredths()
    {
    x  = Math.round(x * 100 + .5) / 100;
    }
    function roundToThousandths()
    {
    x = Math.round(x * 1000 + .5) / 1000;
    }
    document.writeln ("<h1>""x" Rounded to a integer is + roundToInteger() + "<h1>");
    document.writeln ("<h1>""x" Rounded to the tenths place is + roundToTenths() + "<h1>");
    document.writeln ("<h1>""x" Rounded to the hundredths place is + roundToHundredths() + "                   <h1>");
    document.writeln ("<h1>""x" Rounded to the thousandths place is + roundToThousandths() + "<h1>");
</script>
<body>
</body>
</html>

您需要阅读更多javascript。您的代码中充满了错误。正如您在页面(正文)上所写的那样,您的<script>应该在<body>上。此外,当你写<h1>时,你必须在</h1>的末尾关闭它。<title>应位于<head>内部。

无论如何,请阅读我修改您代码的方式:

<!DOCTYPE html>
<html>
    <head>
        <title>The page title is inside head</title>
        <script>
            // Here you can declare your functions
            function roundToInteger(value)
            {
                var new_value = Math.round(value);
                return new_value;
            }
            function roundToTenths(value)
            {
                var new_value = Math.round(value * 10 + .5) / 10;
                return new_value;
            }
            function roundToHundredths(value)
            {
                var new_value = Math.round(value * 100 + .5) / 100;
                return new_value;
            }
            function roundToThousandths(value)
            {
                var new_value = Math.round(value * 1000 + .5) / 1000;
                return new_value;
            }
        </script>
    </head>
    <body>
        <script>
            // Make a pop up box
            var x = window.prompt("Enter a number"); // Lets say you enter 1.623425
            // Use console.log to print errors on the console. (F12>Console)
            console.log('You entered', x);
            document.writeln("<h1>'"x'" Rounded to a integer is " + roundToInteger(x) + "</h1>");
            document.writeln("<h1>'"x'" Rounded to the tenths place is " + roundToTenths(x) + "</h1>");
            document.writeln("<h1>'"x'" Rounded to the hundredths place is " + roundToHundredths(x) + "</h1>");
            document.writeln("<h1>'"x'" Rounded to the thousandths place is " + roundToThousandths(x) + "</h1>");
        </script>
    </body>
</html>

我以前从未见过以这种方式编写函数。你确定这不是问题所在吗?

尝试以这种方式编写函数:

// Set up variables
var x, y;
// Set up function
var gimmeTwo = function(a) {
  a = Math.round(a) + 2;
  return a;
};
// Obtain and clean input
x = window.prompt("Enter a integer");
x = parseInt(x);
// Runthrough function
y = gimmeTwo(x);
// write out
document.write(x + " gimmeTwo is " + y);