Java System.out.print格式 [英] Java System.out.print formatting

查看:153
本文介绍了Java System.out.print格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须显示所有360每月付款,但如果我没有所有的月份数量在同一长度,那么我最终会有一个输出文件,继续向右移动和抵消输出的外观。

Here is my code (well, some of it). The question I have is, can I get the first 9 numbers to show with a leading 00 and numbers 10 - 99 with a leading 0.

I have to show all of the 360 monthly payments, but if I don't have all month numbers at the same length, then I end up with an output file that keeps moving to the right and offsetting the look of the output. 

结果:

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

Results:

我想看的是:

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

What I want to see is:

你需要从我的课堂上看到哪些其他的代码可以帮助你?

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.text.DecimalFormat中;

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

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

另外,您可以使用printf整行完成整个工作:

Alternative you could do whole job in whole line using 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));

这篇关于Java System.out.print格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆