右边用零填充 [英] Right pad with zeros

查看:114
本文介绍了右边用零填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这个问题已经提出,我已经进行了深入搜索,没有任何内容。

Sorry if this question was made already, I've made a deep search and nothing.

现在,我知道:

String.format("%05d", price);

将左边的零填充我的价格,因此价格为25将导致 00025

Will be padding my price with zeros to the left, so a price of 25 will result in 00025

如果我想将它们填充到右边怎么办,结果是 25000 ?如何使用 String.format 模式执行此操作?

What if I want to pad them to the right, so the result is 25000? How do I do that using only String.format patterns?

推荐答案

你可以使用:

String.format("%-5s", price ).replace(' ', '0')

我可以只使用格式模式吗?

Can I do this using only the format pattern?

String.format 使用 Formatter.justify 就像 String.printf 方法。
从这个发布你会看到输出空间是硬编码的,因此必须使用 String.replace

String.format uses Formatter.justify just like the String.printf method. From this post you will see that the output space is hard-coded, so using the String.replace is necessary.

这篇关于右边用零填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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