gpt4 book ai didi

java - 无法对基本类型 double 调用 toFixed(int)

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:06 27 4
gpt4 key购买 nike

我目前很难将第一个条件测试的输出结果四舍五入到小数点后第二位。当数量等于 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: $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>

最佳答案

首先,double calc 是一个原始类型变量,因此没有可用的方法,因为它不是对象。

其次,即使您将Double calc(double基元的包装类)声明为对象,仍然没有名为toFixed()的方法。

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

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

关于java - 无法对基本类型 double 调用 toFixed(int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36519076/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com