代码片断:在m位数之前加0,凑足n位

代码片断:在m位数之间加0,凑足n位.

这主要用于使数字按字符串排序的顺序跟直接按数字排序的顺序相同

比如把 8,9,10,11 变成 08, 09, 10, 11后,按字符串序排序仍与数字大小序相同


	/**
	 * 1 => 0001
	 * 
	 * @param positiveNumber
	 * @param maxValue
	 * @return
	 */
	private static String prependZeroForNumber(int positiveNumber, int maxValue) {
		int digitLength = String.valueOf(maxValue).length();
		String numStr = String.valueOf(positiveNumber);

		StringBuffer pre = new StringBuffer();
		for (int i = 0; i < digitLength - numStr.length(); i++) {
			pre.append("0");
		}
		pre.append(numStr);
		return pre.toString();

	}

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.