【发布时间】:2021-03-16 21:36:11
【问题描述】:
我的学校作业遇到了问题,我们必须为一家快餐公司创建 PoS。 部分任务是在输入投标金额后计算变化。我遇到的问题是程序无法从因“$”而投标的金额中减去总计。我的代码目前如下所示:
private void totalButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Finding the subtotal
double burgers;
double fries;
double drinks;
double subtotal;
burgers = 2.49;
fries = 1.89;
drinks = 0.99;
subtotal = Double.parseDouble (burgerInput.getText ()) * burgers
+ Double.parseDouble (fryInput.getText ()) * fries
+ Double.parseDouble (drinkInput.getText ()) * drinks;
DecimalFormat w = new DecimalFormat("###,###0.00");
subtotalOutput.setText("$" + w.format(subtotal));
// Calculating Tax
double taxpercentage;
double tax;
taxpercentage = 0.13;
tax = subtotal * taxpercentage;
DecimalFormat x = new DecimalFormat("###,###0.00");
taxesOutput.setText("$" + x.format(tax));
// Grand Total
double grandtotal;
grandtotal = subtotal + tax;
DecimalFormat y = new DecimalFormat("###,###0.00");
grandtotalOutput.setText("$" + y.format(grandtotal));
以及计算变化:
// Calculating Change
double tendered;
double grandtotal;
double change;
tendered = Double.parseDouble(tenderedInput.getText ());
grandtotal = Double.parseDouble(grandtotalOutput.getText ());
change = tendered - grandtotal;
DecimalFormat z = new DecimalFormat("###,###0.00");
changeOutput.setText("$" + z.format(change));
如何将“$”保留在 grandtotalOutput 框中,但仍能正确计算更改?
【问题讨论】:
标签: java character symbols gettext