首页 > 后端 > 正文

直接上代码:


class Tool
{
    public function numToExcelLetter($num)
    {
        //由于大写字母只有26个,所以基数为26
        $base = 26;
        $result = '';

        while ($num > 0) {
            $mod = (int)($num % $base);
            $num = (int)($num / $base);

            if ($mod == 0) {
                $num -= 1;
                $temp = $this->numToLetter($base) . $result;
            } elseif ($num == 0) {
                $temp = $this->numToLetter($mod) . $result;
            } else {
                $temp = $this->numToLetter($mod) . $result;
            }
            $result = $temp;
        }

        return $result;
    }

    public function numToLetter($num)
    {
        if ($num == 0) {
            return '';
        }

        $num = (int)$num - 1;
        //获取A的ascii码
        $ordA = ord('A');
        return chr($ordA + $num);
    }
}

调用

    $tool = new Tool();
    $tool->numToExcelLetter(26);

RoveCoder版权所有,转载请注明

猜你喜欢
picture loss