【问题标题】:Java System.out.print formattingJava System.out.print 格式化
【发布时间】:2012-03-24 07:00:56
【问题描述】:

这是我的代码(嗯,其中一些)。我的问题是,我可以让前 9 个数字以 00 开头,数字 10 - 99 以 0 开头。

我必须显示所有 360 个月的付款,但如果我没有相同长度的所有月份数字,那么我最终会得到一个不断向右移动并抵消输出外观的输出文件.

System.out.print((x + 1) + "  ");  // the payment number
System.out.print(formatter.format(monthlyInterest) + "   ");    // round our interest rate
System.out.print(formatter.format(principleAmt) + "     ");
System.out.print(formatter.format(remainderAmt) + "     ");
System.out.println();

结果:

8              $951.23               $215.92         $198,301.22                         
9              $950.19               $216.95         $198,084.26                         
10              $949.15               $217.99         $197,866.27                         
11              $948.11               $219.04         $197,647.23  

我想看到的是:

008              $951.23               $215.92         $198,301.22                         
009              $950.19               $216.95         $198,084.26                         
010              $949.15               $217.99         $197,866.27                         
011              $948.11               $219.04         $197,647.23  

您还需要从我的课程中看到哪些其他代码可以提供帮助?

【问题讨论】:

    标签: java format


    【解决方案1】:

    由于您在其余部分使用格式化程序,因此只需使用 DecimalFormat:

    import java.text.DecimalFormat;
    
    DecimalFormat xFormat = new DecimalFormat("000")
    System.out.print(xFormat.format(x + 1) + " ");
    

    您也可以使用 printf 在整行中完成整个工作:

    System.out.printf("%03d %s  %s    %s    \n",  x + 1, // the payment number
    formatter.format(monthlyInterest),  // round our interest rate
    formatter.format(principleAmt),
    formatter.format(remainderAmt));
    

    【讨论】:

    • 我所做的是使用:import java.text.DecimalFormat;然后我使用了亚当的建议并进行了一些编辑。谢谢亚当! DecimalFormat newFormat = new DecimalFormat("000"); System.out.print(newFormat.format(x + 1) + " "); > 008 $951.23 $215.92 $198,301.22 009 $950.19 $216.95 $198,084.26 010 $949.15 $217.99 $197,866.27 011 $948.11 $219.04 $197,647.23
    【解决方案2】:

    由于您使用的是 Java,printf 从 1.5 版开始提供

    你可以这样使用它

    System.out.printf("%03d ", x);

    例如:

    System.out.printf("%03d ", 5);
    System.out.printf("%03d ", 55);
    System.out.printf("%03d ", 555);
    

    会给你

    005 055 555

    作为输出

    见:System.out.printfFormat String Syntax

    【讨论】:

      【解决方案3】:

      【讨论】:

        【解决方案4】:

        类似的东西

        public void testPrintOut() {
            int val1 = 8;
            String val2 = "$951.23";
            String val3 = "$215.92";
            String val4 = "$198,301.22";
            System.out.println(String.format("%03d %7s %7s %11s", val1, val2, val3, val4));
        
            val1 = 9;
            val2 = "$950.19";
            val3 = "$216.95";
            val4 = "$198,084.26";
            System.out.println(String.format("%03d %7s %7s %11s", val1, val2, val3, val4));
        }
        

        【讨论】:

          【解决方案5】:

          您确定要“055”而不是“55”吗?一些程序将前导零解释为八进制,因此它将 055 读作(十进制)45 而不是(十进制)55。

          这应该只是意味着删除“0”(零填充)标志。

          例如,将System.out.printf("%03d ", x); 更改为更简单的System.out.printf("%3d ", x);

          【讨论】:

            【解决方案6】:

            只需使用\t 来分隔它。

            例子:

            System.out.println(monthlyInterest + "\t")
            
            //as far as the two 0 in front of it just use a if else statement. ex: 
            x = x+1;
            if (x < 10){
                System.out.println("00" +x);
            }
            else if( x < 100){
                System.out.println("0" +x);
            }
            else{
                System.out.println(x);
            }
            

            还有其他方法可以做到,但这是最简单的。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-07-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-02-22
              相关资源
              最近更新 更多