简体   繁体   English

输出格式化Java

[英]output formatting Java

my question is simple, how can i get my output to format two values with two decimal places. 我的问题很简单,我如何获取输出以格式化两个带有两位小数位的值。 for whatever reason i cant output the "current balance" & "new balance" with two decimal places. 无论出于什么原因,我都无法输出带有两个小数位的“当前余额”和“新余额”。 separately they work fine but when together i get a conversion error. 他们分别工作正常,但当我在一起时遇到转换错误。 is this a rule i'm unaware of? 这是我不知道的规则吗? i would like do do this with one action if possible. 如果可能的话,我想采取一项行动。 this is simply a cosmetic issue and all operations perform fine when i take out the formatting. 这只是一个表面问题,当我取出格式化文件时,所有操作都执行良好。

thanks, 谢谢,

public static void main(String[] args) 
{
    double min;
    int accNum;
    char accType;
    double bal;
    double newBal;

    //user inputs
    System.out.println("Please enter your account number: ");
    accNum = console.nextInt();
    System.out.println();

    System.out.println("Enter the account type: s(savings) or c(checking)");
    accType = console.next().charAt(0);
    System.out.println();

    //savings v checking
    switch (accType)
    {
        case 's':
        case 'S':
            System.out.println("Enter the current balance: ");
            bal = console.nextDouble();

            System.out.println("Enter the minimum balance: ");
            min = console.nextDouble();
            System.out.println();

            if (bal < min)
            { 
                newBal = bal - S_MIN_FEE;
                System.out.println("Insufficient Funds (-$10.00)");
            }
            else
                newBal = bal + (bal * S_INT);

            System.out.printf("Account #: " + accNum + "\n" 
                    + "Account Type: Savings" + "\n" + "Current Balance: "
                    + "$%.2f%n", bal + "New Balance: $%.2f", newBal);
        case 'c':
        case 'C':
            System.out.println("Enter the current balance: ");
            bal = console.nextDouble();

            System.out.println("Enter the minimum balance: ");
            min = console.nextDouble();
            System.out.println();

            if (bal < min)
            {
                newBal = bal - C_MIN_FEE;
                System.out.println("Insufficent Funds (-$25.00)");
            }
            else if (bal < C_BAL_MAX && bal >= min)
                newBal = bal + (bal * C_INT);
            else
                newBal = bal + (bal * C_INT_MAX);

            System.out.printf("Account #: " + accNum + "\n" 
                    + "Account Type: Checking" + "\n" + "Current Balance: "
                    + "$%.2f%n", bal + "New Balance: $%.2f%n", newBal);

That is because you put the format first, and only first. 这是因为您将格式放在第一位,并且仅放在第一位。

System.out.printf("%.2f %.2f%n", bal, newBal);

This is the same problem/mistake as posted an hour ago. 这与一个小时前发布的问题/错误相同。 java.util.IllegalFormatConversionException: f != java.lang.String Error java.util.IllegalFormatConversionException:f!= java.lang.String错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM