Home > Java > javaTutorial > Implementation code for Java String string padding with 0s or spaces

Implementation code for Java String string padding with 0s or spaces

高洛峰
Release: 2017-01-22 11:22:53
Original
2031 people have browsed it

废话不多说了,关键代码如下所示:

package cn.com.songjy;
import java.text.NumberFormat;
//Java 中给数字左边补0
public class NumberFormatTest {
public static void main(String[] args) {
// 待测试数据
int i = 1;
// 得到一个NumberFormat的实例
NumberFormat nf = NumberFormat.getInstance();
// 设置是否使用分组
nf.setGroupingUsed(false);
// 设置最大整数位数
nf.setMaximumIntegerDigits(4);
// 设置最小整数位数
nf.setMinimumIntegerDigits(4);
// 输出测试语句
System.out.println(nf.format(i));
}
}
/** 
* Java里数字转字符串前面自动补0的实现。 
* 
*/
public class TestStringFormat { 
public static void main(String[] args) { 
int youNumber = 1; 
// 0 代表前面补充0 
// 4 代表长度为4 
// d 代表参数为正数型 
String str = String.format("%04d", youNumber); 
System.out.println(str); // 0001 
} 
}
//流水号加1后返回,流水号长度为4
private static final String STR_FORMAT = "0000"; 
public static String haoAddOne_2(String liuShuiHao){
Integer intHao = Integer.parseInt(liuShuiHao);
intHao++;
DecimalFormat df = new DecimalFormat(STR_FORMAT);
return df.format(intHao);
}
Copy after login

好了,以上代码就是关于Java String字符串补0或空格的实现代码,非常不错,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对PHP中文网的支持!

更多Java String字符串补0或空格的实现代码相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template