如何打印带有数字结尾的语句,但将数字从右向左设置为特定长度?

huangapple go评论66阅读模式
英文:

How would I print a statement with a number at the end, but set the number from right to left at a certain lenght?

问题

我对编程非常陌生,除了简单的格式设置和布尔语句外,我一无所知。我想要打印的内容是:

总工资:     $   0.00

我的代码是:

System.out.printf("总工资:     " + "$" + "%.2f\n", grossPay);

输出结果是:

总工资:     $0.00

我相信我应该从右到左打印,长度为7个空格。我应该如何做到这一点?

编辑:抱歉,我没有提供更多具体细节。我不是在询问代码是否有效,而是在问如何使输出完全符合 总工资: $ 0.00,保留所有的空格。我的实际问题是如何让 $ 0.00,在 $ 和 0.00 之间的 3 个空格是自动的,而不仅仅是用 + " " +

英文:

I'm very new to coding, and this I don't know anything beyond simple formatting and Boolean statement. What I'm trying to print is:

Gross Pay:     $   0.00

My code is:

System.out.printf("Gross Pay:     " + "$" + "%.2f\n", grossPay);

and the output is

Gross Pay:     $0.00

I believe I'm suppose to print from right to left with 7 space length. How would I do that?

EDIT: Sorry I wasn't more specific. I wasn't necessary asking for if it's valid or not, but I was asking how to get it to be exactly Gross Pay: $ 0.00 with the space and all. My actual question is how to get the $ 0.00, having the space 3 between $ and 0.00 being automatic as oppose to just doing + " " + .

答案1

得分: 1

以下是翻译好的内容:

类似这样的代码应该可以工作(已编辑):

String.format("%7.2f", value)

示例:

System.out.println("Gross Pay:     $" + String.format("%7.2f", 10.01));
System.out.println("Gross Pay:     $" + String.format("%7.2f", 100.01));
System.out.println("Gross Pay:     $" + String.format("%7.2f", 1000.01));
System.out.println("Gross Pay:     $" + String.format("%7.2f", 10000.01));

示例输出:

Gross Pay:     $  10.01
Gross Pay:     $ 100.01
Gross Pay:     $1000.01
Gross Pay:     $10000.01

作为使用数字填充的替代方法,以下是一个带有数字填充的示例:

DecimalFormat currency = new DecimalFormat();
currency.setGroupingUsed(false);
currency.setMinimumIntegerDigits(12);
currency.setMaximumIntegerDigits(12);
currency.setMinimumFractionDigits(2);
currency.setMaximumFractionDigits(2);
currency.setDecimalSeparatorAlwaysShown(true);
System.out.println("Gross Pay:     $" + currency.format(939394.02480240));
英文:

Something like this should work (Edited):

String.format("%7.2f", value)

Sample:

System.out.println("Gross Pay:     $" + String.format("%7.2f", 10.01));
System.out.println("Gross Pay:     $" + String.format("%7.2f", 100.01));
System.out.println("Gross Pay:     $" + String.format("%7.2f", 1000.01));
System.out.println("Gross Pay:     $" + String.format("%7.2f", 10000.01));

Sample Output:

Gross Pay:     $  10.01
Gross Pay:     $ 100.01
Gross Pay:     $1000.01
Gross Pay:     $10000.01

As an alternative to using spaces, this is a sample with number padding:

        DecimalFormat currency = new DecimalFormat();
		currency.setGroupingUsed(false);
		currency.setMinimumIntegerDigits(12);
		currency.setMaximumIntegerDigits(12);
		currency.setMinimumFractionDigits(2);
		currency.setMaximumFractionDigits(2);
		currency.setDecimalSeparatorAlwaysShown(true);
		System.out.println("Gross Pay:     $" + currency.format(939394.02480240));

答案2

得分: 0

可以这样打印的话也是有效的:System.out.printf("Gross Pay: " + "$ " + "%.2f\n", grossPay);

英文:

It could be valid if you print it like this: System.out.printf("Gross Pay: " + "$ " + "%.2f\n", grossPay); ?

huangapple
  • 本文由 发表于 2020年9月21日 23:09:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63995098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定