我的任務要求我將值格式化爲兩位小數。我們被要求使用JOptionPane進行輸入/輸出。我知道如何使用System.out.printf格式化爲兩位小數。但是,我認爲這不會起作用,因爲我們需要使用JOptionPane。如何通過JOptionPane格式化十進制數的輸出
如果任何人都可以給我任何關於如何格式化字符串的建議(這樣我就可以將字符串解析成雙精度),我會很感激。謝謝。
這裏是我的代碼如下:
package Program4;
import javax.swing.*;
public class Program4 {
public static void main (String[] args)
{
//Declaration of Variables
Double AssessedValue;
Double TaxableAmount;
Double PropertyTax;
Double TaxRatefor100 = 1.05;
String StrAssessedValue;
String OutputStr;
//Ask the user for the Assessed Value of their property
StrAssessedValue = JOptionPane.showInputDialog("Enter the assessed " +
"value of your property: ");
//Convert the string into a double
AssessedValue = Double.parseDouble(StrAssessedValue);
//Calculations
TaxableAmount = 0.92*AssessedValue;
PropertyTax = (TaxableAmount/100)*1.05;
//Store the output in a string
OutputStr = "Assessed Value: $" + AssessedValue + "\n" +
"Taxable Amount: $" + TaxableAmount + "\n" +
"Tax Rate for each $100.00: $" + TaxRatefor100 +
"\n" + "Property Tax: $" + PropertyTax;
//Output the result
JOptionPane.showMessageDialog(null, OutputStr, "Property Tax Values",
JOptionPane.WARNING_MESSAGE);
}
}
+1 Two-answers-in-one! – trashgod