您的位置:首页 > 编程语言 > Java开发

java 补零

2016-06-08 15:29 363 查看
//在前用3个零补齐  测试调用
System.out.println("----"+fillZero("105", 3));
//在前用3个零补齐   测试调用

System.out.println("----"+fillZero("1", 3));

/** * 装位数不够totalBit的补0 */
public static String fillZero(int value, int totalBit)
{return fillZero(String.valueOf(value), totalBit);}

public static String fillZero(String value, int totalBit) {
int length = value.length();
int fillLength = totalBit - length;
if(fillLength < 0) throw new MyRuntimeException("com.project.framework.util.ProjectUtils - fillZero: value.length()>totalBit!");
String returnValue = "";
for(int i=0; i<fillLength; i++) {returnValue += "0";}
returnValue += value;
return returnValue;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java