项目上的需求,需要把一个指定的列号(整型)换算为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
转载于:https://blog.51cto.com/pep525/554370