Java字符串右对齐 [英] Java string align to right

查看:753
本文介绍了Java字符串右对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些数字的数组

61672
8414449
264957

我使用 DecimalFormat 这样的对象

DecimalFormat formatter = new DecimalFormat("###,### bytes");

获得这些结果

61,672 bytes
8,414,449 bytes
264,957 bytes

但我需要将结果与以下对齐:

but I need the results to be aligned to right like the following

   61,672 bytes
8,414,449 bytes
  264,957 bytes

我们已经感谢您的帮助。

Your help is already appreciated.

推荐答案

你可以将它包装成 String.format 这样的调用:

You can wrap it into a String.format call like this:

String.format("%15s", formatter.format(i))

这篇关于Java字符串右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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