无法在基本类型 double 上调用 toFixed(int)

Cannot invoke toFixed(int) on the primitive type double

我目前很难将第一个条件测试的输出结果四舍五入到小数点后第二位。当 quant 等于 5 时,将其乘以两个选项中的任何一个,结果得出小数点右边有 6+ 个空格的数字。当我尝试将 .toFixed 附加到任何东西时,它声明它不能将 toFixed(int) 应用于原始类型 double,即使我放置了一个链接到 double 的新字符串变量。

我只是想将输出四舍五入到小数点后第二位。任何帮助将不胜感激。

更新!!! 感谢 muzahidbechara 的协助。他建议放置代码

String result = String.valueOf(new BigDecimal(calc).setScale(2, BigDecimal.ROUND_HALF_UP));

经过一番摸索之后,在 muzahidbechara 的额外帮助下,我发现需要删除代码,因为存在我不知道的优先顺序。

再次感谢 muzahidbechara 的帮助。如果其他人有这种麻烦,请给他的答案投票。

我将代码放在下面的 .jsp 文件中,这样您就可以看到我把它放在哪里了。总体输出尚未完成(就 html 代码而言)是我明天要处理的事情。

我放在 Tomcat webapp/root 文件夹中的 HTML 文件

<!doctype html>
<html>
    <head>
        <!--
            Name: Student
            Date: 07APR16
            Program Description: Movie Rental Scenario
        -->

        <style>
            body {font-family:arial;}

            table.inner {border:2px solid black;margin-left: auto;margin-right: auto;background-color:rgb(128,170,255);}

            table.outer {border:2px solid black;margin-left: auto;margin-right: auto;}

            th {font-weight:normal;font-variant:small-caps;}

            input {background-color:rgb(204,230,255);}

            input.number {text-align:center;}

            p.top {font-weight:bold;font-size:15px;font-variant:small-caps;}

            p.bottom {text-align:"center";font-weight:bold;font-style:italic;font-size:15px;}

            input.footer {margin-left: auto;margin-right: auto}

            h5 {text-decoration:underline;}

            h5.misc1 {margin:0px 0px -10px 0px;}

            h5.misc2 {margin:0px 0px -20px 0px;}


        </style>
    </head>

    <body>
                <table class="outer" cellpadding="10px" border="1">
                <tr>
                    <th>
        <form method="post" action="videorental.jsp">
        <p class="top">Please provide contact information in the fields below</p>
            <table class="inner" cellpadding="10px" border="1">
                <tr>
                    <th>
                        <input type="text" id="fname" name="fname" placeholder="Firstname" required />
                        <input type="text" id="lname" name="lname" placeholder="Lastname" required /><br />
                        <br />
                        <input type="text" id="email" name="email" placeholder="email@example.net" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" title="The email address you entered is invalid, please check to ensure you're using a valid email address that fits the formate email@example.net" style="width:288px;" required /><br />
                    </th>
                </tr>
            </table>
            <br />
        <p class="bottom">Please provide additional information below</p>
            <table class="inner" cellpadding="10px" border="1">
                <tr>
                    <th>
                    <h5 class="misc1">Number of movies to rent</h5><br />
                    <input class="number" type="text" name="quant" placeholder="##" pattern="[0-9]{1,2}" title="Please input a number between 1-99" style="width:20px" required/><br />
                    <br />
                    <h5 class="misc2">Which type of movie<h5>
                    <input type="radio" id="type" name="type" value="1" required />DVD (Cost per: .99)<br />
                    <input type="radio" id="type" name="type" value="2" required />Blu-ray (Cost per: .99)<br />
                    </th>
                </tr>
            </table>
            <br />
                <input class="footer" type="submit" value="Submit" />
                <input type="reset" value="Reset" />
                                    </th>
                </tr>
            </table>
        </form>
    </body>
</html>

我的服务器端 Javascript 我放在 Tomcat webapp/root 文件夹中的文件

<!DOCTYPE html>
    <html>
    <head>
    <style>

        body {font-family:arial;}

        table {border:2px solid black;margin-left: auto;margin-right: auto;background-color:rgb(128,170,255);}

        th {font-weight:normal;font-variant:small-caps;padding-left:500px;text-align:right;}

        td {text-align:center;}

    </style>
    </head>

    <body>
    <%
        /*
            Name: Student
            Date: 07APR16
            Program Description: Movie Rental Scenario
        */

        //Received Data

        String first = request.getParameter("fname");
        String last = request.getParameter("lname");
        String mail = request.getParameter("email");

        double num1 = Double.parseDouble(request.getParameter("quant"));
        byte choice = Byte.parseByte(request.getParameter("type"));

        double calc = 0.0;
        double answer = 0.0;

        // Condition Testing 1
        if (choice == 1)
        {
            calc = num1 * 2.99;
        }
        else if (choice == 2)
        {
            calc = num1 * 3.99;
        }

        // Condition Testing 2

        if (choice == 1)
        {
            answer = 2.99;
        }
        else if (choice == 2)
        {
            answer = 3.99;
        }

        String result = String.valueOf(new java.math.BigDecimal(calc).setScale(2, java.math.BigDecimal.ROUND_HALF_UP));

        //Display Results

        out.print("First-name  = " + first);
        out.print("Last-name = " + last);
        out.println("<BR>");
        out.print("Email Address = " + mail);
        out.println("<table border=1>");
        out.println("<tr>");
        out.println("<th>");
        out.print("Price for type of rental:<td>" + answer);
        out.println("</td>");
        out.println("</th>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<th>");
        out.print("Number of movies to rent:<td>" + num1);
        out.println("</td>");
        out.println("</th>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<th>");
        out.println("<td>");
        out.println("==========");
        out.println("</td>");
        out.println("</th>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<th>");
        out.print("Total:<td>" + result);
        out.println("</td>");
        out.println("</th>");
        out.println("</tr>");
        out.println("</table>");
    %>
</body>

首先double calc是原始类型变量,所以没有方法可用,因为它不是Object。

其次,即使您将 Double calc(双基元的包装器 class)声明为对象,仍然没有名为 toFixed().

的方法

您可以使用 java.math.BigDecimal 来实现像 Javascript 这样的精度 toFixed():

String result = String.valueOf(new BigDecimal(calc).setScale(2, BigDecimal.ROUND_HALF_UP));