I'm currently having difficulty getting my output results from my first condition testing to be rounded to the 2nd decimal place. When the quant is equal to 5, and it is multiplied by either of the two choices, the result comes out to a number with 6+ spaces right of the decimal point. When I try to attach the .toFixed to anything, it states that it cannot apply toFixed(int) to the primitive type double, even when I place a new string variable linking to the double.
I'm just trying to get the output rounded to the 2nd decimal point. Any help would be greatly appreciated.
UPDATE!!! Thanks to muzahidbechara for the assist. His suggestion of placing the code
String result = String.valueOf(new BigDecimal(calc).setScale(2, BigDecimal.ROUND_HALF_UP));
After some flopping around, I found out with additional help from muzahidbechara that the code needed to be brought down since there is an order of precedence that I didn't know about.
So thanks again to muzahidbechara for the help. If anybody else had this kind of trouble, please up vote his answer.
I placed the code in the .jsp file below so you can see where I put it. The overall output isn't complete yet (as far as the html code is concerned) is something I'll be working on tomorrow.
My HTML File that I place in my Tomcat webapp/root folder
<!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>
My Server Side Javascript File that I place in my Tomcat webapp/root folder
<!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>
First of all double calc
is a primitive type variable, so there is no method available because it's not a Object.
Second, even if you would declare Double calc
(Wrapper class of double primitive)as a Object still there is no method named toFixed()
.
You could use java.math.BigDecimal
to achieve precision like Javascript toFixed()
:
String result = String.valueOf(new BigDecimal(calc).setScale(2, BigDecimal.ROUND_HALF_UP));