我想将一个3位数的integer
格式化为4位数的string
值。示例:
int a = 800;
String b = "0800";
当然,格式化将在String b
语句中完成。谢谢你们!
发布于 2013-07-17 18:10:43
使用String#format
String b = String.format("%04d", a);
有关其他格式,请参阅documentation
发布于 2013-07-17 18:19:26
如果你想让它只有一次,使用String.format("%04d", number)
-如果你经常需要它,并且想要集中模式(例如配置文件),请看下面的解决方案。
顺便说一句。有一个关于数字格式化的Oracle tutorial。
长话短说:
import java.text.*;
public class Demo {
static public void main(String[] args) {
int value = 123;
String pattern="0000";
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);
System.out.println(output); // 0123
}
}
希望这能有所帮助。*Jost
发布于 2013-07-17 18:10:02
String b = "0" + a;
能不能更简单一点呢?
https://stackoverflow.com/questions/17696946
复制相似问题