我目前很难将我的第一个条件测试的输出结果四舍五入到小数点后第二位。当quant等于5,并且它乘以两个选项中的任何一个时,结果将出现在小数点右侧有6个以上空格的数字。当我尝试将.toFixed附加到任何东西时,它声明它不能将固定(int)应用于基本类型double,即使我将一个新的字符串变量链接到double。
我只是试图将输出四舍五入到第二个小数点。任何帮助将不胜感激。
UPDATE !!! 感谢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: $2.99)<br />
<input type="radio" id="type" name="type" value="2" required />Blu-ray (Cost per: $3.99)<br />
</th>
</tr>
</table>
<br />
<input class="footer" type="submit" value="Submit" />
<input type="reset" value="Reset" />
</th>
</tr>
</table>
</form>
</body>
</html>
我放在Tomcat webapp / root文件夹中的服务器端Javascript文件
<!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>
答案 0 :(得分:0)
首先,double calc
是一个原始类型变量,所以没有可用的方法,因为它不是一个Object。
其次,即使您将Double calc
(双层原语的Wrapper类)声明为Object仍然没有名为toFixed()
的方法。
您可以使用java.math.BigDecimal
来实现Javascript toFixed()
的精确度:
String result = String.valueOf(new BigDecimal(calc).setScale(2, BigDecimal.ROUND_HALF_UP));