Java 我的杂货店账单计划在总数中增加了一个不必要的小数字
我记得这是一个我可能遇到的问题,但我忘记了原因。这是我的密码Java 我的杂货店账单计划在总数中增加了一个不必要的小数字,java,Java,我记得这是一个我可能遇到的问题,但我忘记了原因。这是我的密码 import java.util.Scanner; public class GroceryTab { public static void main(String[] args) { double total = 0; int items = 0; System.out.print("How many different products are you b
import java.util.Scanner;
public class GroceryTab
{
public static void main(String[] args)
{
double total = 0;
int items = 0;
System.out.print("How many different products are you buying?");
Scanner in = new Scanner(System.in);
items = in.nextInt();
for(int i=1; i<=items; i++) {
double price;
int numberBought;
System.out.print("What is the price of your " + i +"th item?");
Scanner priceIn = new Scanner(System.in);
price = priceIn.nextDouble();
System.out.print("How many of this item are you buying?");
Scanner numIn = new Scanner(System.in);
numberBought = numIn.nextInt();
total += (price * numberBought);
}
System.out.print("Your list costs " + total + " dollars.");
}
}
import java.util.Scanner;
公共级食品杂货店
{
公共静态void main(字符串[]args)
{
双倍合计=0;
整数项=0;
System.out.print(“您购买了多少种不同的产品?”);
扫描仪输入=新扫描仪(系统输入);
items=in.nextInt();
因为(int i=1;i浮点(price
和total
在这里是double
s)是不精确的;如果您想保持价格更精确,一个可能的解决方法是将价格跟踪为int
s(可能是#美分)。(price
和total
在这里是double
s)是不精确的;如果你想保持价格更精确,一个可能的解决办法是将价格跟踪为int
s(可能是#美分)。我只想评论一下,但没有代表
这是因为您使用的是double(通常是浮点)。如果您需要精确精度,BigDecimal更好,但速度较慢
请看我只想发表评论,但没有代表性
这是因为您使用的是double(通常是浮点)。如果您需要精确精度,BigDecimal更好,但速度较慢
请看它必须处理双精度。我建议使用
DecimalFormat df = new DecimalFormat("#.00");
System.out.print("Your list costs " + df.format(total) + " dollars.");
或者类似的东西,因为你用的是美元,不会想要。哦哦哦1美分。无论如何,这不是你的问题。只是双精度不是最好的。它必须处理双精度。我建议使用
DecimalFormat df = new DecimalFormat("#.00");
System.out.print("Your list costs " + df.format(total) + " dollars.");
或者类似的东西,因为你使用的是美元,不会想要。哦哦哦1美分。无论如何,这不是你的问题。只是双精度不是最好的。使用BigDecimal并正确执行:
BigDecimal total = BigDecimal.ZERO;
System.out.print("How many different products are you buying?");
Scanner in = new Scanner(System.in);
int items = in.nextInt();
for (int i=1; i<=items; i++) {
System.out.print("What is the price of your " + i +"th item?");
Scanner priceIn = new Scanner(System.in);
BigDecimal price = priceIn.nextBigDecimal();
System.out.print("How many of this item are you buying?");
Scanner numIn = new Scanner(System.in);
int numberBought = numIn.nextInt();
BigDecimal lineTot = price.multiply( BigDecimal.valueOf(numberBought));
total = total.add( lineTot);
}
System.out.print("Your list costs " + total + " dollars.");
BigDecimal总计=BigDecimal.ZERO;
System.out.print(“您购买了多少种不同的产品?”);
扫描仪输入=新扫描仪(系统输入);
int items=in.nextInt();
对于(int i=1;i使用BigDecimal并正确执行:
BigDecimal total = BigDecimal.ZERO;
System.out.print("How many different products are you buying?");
Scanner in = new Scanner(System.in);
int items = in.nextInt();
for (int i=1; i<=items; i++) {
System.out.print("What is the price of your " + i +"th item?");
Scanner priceIn = new Scanner(System.in);
BigDecimal price = priceIn.nextBigDecimal();
System.out.print("How many of this item are you buying?");
Scanner numIn = new Scanner(System.in);
int numberBought = numIn.nextInt();
BigDecimal lineTot = price.multiply( BigDecimal.valueOf(numberBought));
total = total.add( lineTot);
}
System.out.print("Your list costs " + total + " dollars.");
BigDecimal总计=BigDecimal.ZERO;
System.out.print(“您购买了多少种不同的产品?”);
扫描仪输入=新扫描仪(系统输入);
int items=in.nextInt();
对于(int i=1;java浮点运算会导致这种问题,每个人都需要学习浮点运算。在处理money时,您可能应该创建一个money类来正确处理舍入或使用BigDecimal,而不是float或double。您看到的是浮点运算的不精确性存储的是y浮点数,有些浮点数没有精确的表示形式。如果直接或通过一系列操作得到其中一个浮点数,则需要对其进行适当的舍入。使用Money类或直接将值作为十进制数处理的类可以解决此问题。java浮点数将导致k每个人都需要学习浮点运算。当处理货币时,你可能应该创建一个正确处理舍入或使用BigDecimal的货币类,而不是浮点数或double。你看到的是浮点运算的不精确性。浮点数的存储方式,有些是没有的精确表示法。如果直接或通过一系列操作得到这些数字中的一个,则需要对其进行适当的舍入。使用Money类或直接将值作为十进制数处理的类可以解决此问题。大十进制是正确答案,按比例整数几乎肯定不是。我们没有这样做这里4k内存中的汇编语言。如果你在跟踪价格,那么跟踪美分需要额外两个小数位。在这一点上,你是想使用BigDecimal还是只在内部保留美分更多的是一个风格问题。如果这是一个有几个小数位的用例,我同意你的看法。在风格上,我倾向于使用en封装、未来证明和正确存储到JDBC/SQL数据库中。我做了很多业务应用程序工作,使用BigDecimal是绝对标准和必需的。当然,我也同意。同时,我认为我的答案不值得否决,因为它确实解决了OP关于原始输出为何略有偏差的问题。我威尔承认我的答案不值一票。谢谢你礼貌的反馈,丹尼斯。乐意效劳:)BigDecimal是正确的答案,scaled Int几乎肯定不是。我们这里不是在4k内存中使用汇编语言。如果你在跟踪价格,则需要额外两位小数来跟踪美分。在这一点上,你是想使用BigDecimal还是只在内部保留美分更是一个风格问题。这是不是比如说,有几个小数位的用例,我同意你的看法。在风格上,我倾向于封装、未来证明和正确存储到JDBC/SQL数据库中。我做了很多业务应用程序工作,使用BigDecimal是绝对标准和必需的。当然,我也同意。同时,我认为我的答案不值得一提e因为它确实解决了OP的问题,即为什么原始输出略有下降。我承认我的答案不值得一票。谢谢你礼貌的反馈,丹尼斯。很乐意效劳:)BigDecimal是Java中固定精度货币工作的唯一正确解决方案。甚至不要浪费时间在scaled int之类的黑客上——只要做得恰当就行了。(BigDecimal在内部是一个scaled int,所以为什么要浪费时间复制这项工作——更糟——毫无好处——对我来说是个谜。)明白了。删除了用美分计算美元的建议。你可以这样回答。BigDecimal是Java中固定精度货币工作的唯一正确解决方案。不要浪费时间在scale之类的黑客上