如何通过 JOptionPane 设置十进制数字输出的格式

发布于 2024-12-10 03:01:50 字数 1507 浏览 0 评论 0原文

我的作业要求我将值的格式设置为小数点后两位。我们被要求使用 JOptionPane 进行输入/输出。我知道如何使用 System.out.printf 将格式设置为两位小数。但是,我认为这行不通,因为我们需要使用 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);           
        }
}

My assignment asks me to format values to two decimal places. We are asked to use the JOptionPane for input/output. I know how to format to two decimal places using the System.out.printf. However, I don't think that would work, because we need to use the JOptionPane.

If anyone could give me any advice on how to format it in a string (so I could then parse the string into a double), I'd appreciate it. Thanks.

Here is my code as follows:

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);           
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一指流沙 2024-12-17 03:01:50
String.format("%.2f", myNumber);

或者

DecimalFormat decFormat = new DecimalFormat("0.00");
String.format("%.2f", myNumber);

or

DecimalFormat decFormat = new DecimalFormat("0.00");
知足的幸福 2024-12-17 03:01:50

对于输入,您可以考虑使用 JSpinner 具有适当的 SpinnerNumberModel,或 JFormattedTextField

For the input, you might consider using a JSpinner with an appropriate SpinnerNumberModel, or a JFormattedTextField.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文