項目上的需求,需要把一個指定的列號(整型)換算爲Excel的字符列號(類似於AA、AB等),於是寫了下面這個類,可支持任意自定義進制,比如3進制,4進制等等諸如此類,並支持設置起始最小值,起始Excel的列號就是最小值爲1的27進制,代碼如下:
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 自定義進制類
- *
- * @category 框架工具實體類
- * @author 王鍾沛
- * @version 1.0
- */
- public class CustomDigit {
- /** 十進制數 */
- private Integer decimal;
- /** 用於儲存轉換後數據的列表 */
- private List<CustomInteger> numberList = new ArrayList<CustomInteger>();
- /** 位數,比如2、8、10、16、60等等任意正整數 */
- private Integer digit;
- /** 最小值,默認爲0 */
- private Integer minValue = 0;
- public CustomDigit() {
- super();
- }
- public CustomDigit(Integer decimal, Integer digit) {
- this.decimal = decimal;
- this.digit = digit;
- numberList.add(new CustomInteger(0));
- }
- public CustomDigit(Integer decimal, Integer digit, Integer minValue) {
- this.decimal = decimal;
- this.digit = digit;
- this.minValue = minValue;
- numberList.add(new CustomInteger(0));
- }
- /**
- * @return the decimal
- */
- public Integer getDecimal() {
- return decimal;
- }
- /**
- * @param decimal
- * the decimal to set
- */
- public void setDecimal(Integer decimal) {
- this.decimal = decimal;
- }
- /**
- * @return the numberList
- */
- public List<CustomInteger> getNumberList() {
- return numberList;
- }
- /**
- * @param numberList
- * the numberList to set
- */
- public void setNumberList(List<CustomInteger> numberList) {
- this.numberList = numberList;
- }
- /**
- * @return the digit
- */
- public Integer getDigit() {
- return digit;
- }
- /**
- * @param digit
- * the digit to set
- */
- public void setDigit(Integer digit) {
- this.digit = digit;
- }
- /**
- * @return the minValue
- */
- public Integer getMinValue() {
- return minValue;
- }
- /**
- * @param minValue
- * the minValue to set
- */
- public void setMinValue(Integer minValue) {
- this.minValue = minValue;
- }
- public void clear() {
- this.decimal = Integer.valueOf(0);
- this.digit = Integer.valueOf(0);
- this.minValue = Integer.valueOf(0);
- this.numberList.clear();
- }
- public Integer getLength() {
- return this.numberList.size();
- }
- private List<CustomInteger> formattedNumberList() {
- int initialNumber = decimal.intValue();
- while (initialNumber > 0) {
- int tempNumber = initialNumber > digit.intValue() ? digit
- .intValue() : initialNumber;
- CustomInteger lastNumber = this.numberList.get(getLength() - 1);
- lastNumber.setValue(lastNumber.getValue() + tempNumber);
- initialNumber = initialNumber - tempNumber;
- arrangementList(getLength() - 1);
- }
- return this.numberList;
- }
- private void arrangementList(Integer index) {
- CustomInteger lastNumber = this.numberList.get(index);
- if (lastNumber.getValue() >= digit) {
- boolean flag = false;
- int value = lastNumber.getValue() - digit + minValue;
- if(value >= digit){
- flag = true;
- // 防止+minValue之後又再次大於進制的閥值
- value = value - digit + minValue;
- }
- // 如果再次大於閥值,則向上一位進2,否則進1
- int count = flag ? 2 : 1;
- lastNumber.setValue(value);
- if (index.intValue() == 0) {
- this.numberList.add(0, new CustomInteger(count));
- return;
- } else {
- CustomInteger preNumber = this.numberList.get(index - 1);
- preNumber.setValue(preNumber.getValue() + count);
- }
- }
- if (index.intValue() == 0) {
- return;
- }
- arrangementList(index - 1);
- }
- public List<Integer> getFormattedNumberList() {
- List<Integer> list = new ArrayList<Integer>();
- this.formattedNumberList();
- for (CustomInteger ci : this.numberList) {
- list.add(ci.getValue());
- }
- return list;
- }
- }
- class CustomInteger {
- private Integer value;
- public CustomInteger() {
- }
- public CustomInteger(Integer value) {
- this.value = value;
- }
- public Integer getValue() {
- return value;
- }
- public void setValue(Integer value) {
- this.value = value;
- }
- public String toString() {
- return String.valueOf(value);
- }
- }
測試代碼如下:
- public static void main(String[] args) {
- CustomDigit cd = new CustomDigit(750,27,1);
- for(Integer i :cd.getFormattedNumberList()){
- // 因爲從1開始,所以這邊只加上64,即從'A'輸出到'Z'
- System.out.print((char)(i+64));
- }
- }
第一個參數爲10進制的數字,第二個參數爲進制,第三個參數爲該進制下的最小值,當最小值爲0時可省略第三個參數
上述測試程序輸出結果爲:
- ABV