久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

獲取數(shù)字的類似excel的列名的算法

Algorithm to get the excel-like column name of a number(獲取數(shù)字的類似excel的列名的算法)
本文介紹了獲取數(shù)字的類似excel的列名的算法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在編寫一個生成一些 Excel 文檔的腳本,我需要將一個數(shù)字轉(zhuǎn)換為其等效的列名.例如:

I'm working on a script that generate some Excel documents and I need to convert a number into its column name equivalent. For example:

1 => A
2 => B
27 => AA
28 => AB
14558 => UMX

我已經(jīng)編寫了一個算法來這樣做,但我想知道是否有更簡單或更快的方法來做到這一點:

I have already written an algorithm to do so, but I'd like to know whether are simpler or faster ways to do it:

function numberToColumnName($number){
    $abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $abc_len = strlen($abc);

    $result_len = 1; // how much characters the column's name will have
    $pow = 0;
    while( ( $pow += pow($abc_len, $result_len) ) < $number ){
        $result_len++;
    }

    $result = "";
    $next = false;
    // add each character to the result...
    for($i = 1; $i<=$result_len; $i++){
        $index = ($number % $abc_len) - 1; // calculate the module

        // sometimes the index should be decreased by 1
        if( $next || $next = false ){
            $index--;
        }

        // this is the point that will be calculated in the next iteration
        $number = floor($number / strlen($abc));

        // if the index is negative, convert it to positive
        if( $next = ($index < 0) ) {
            $index = $abc_len + $index;
        }

        $result = $abc[$index].$result; // concatenate the letter
    }
    return $result;
}

你知道更好的方法嗎?也許是為了讓它更簡單?還是性能改進?

Do you know a better way to do it? Maybe something to keep it simpler? or a performance improvement?

ircmaxell 的實現(xiàn)效果很好.但是,我要添加這個簡短的內(nèi)容:

ircmaxell's implementation works pretty fine. But, I'm going to add this nice short one:

function num2alpha($n)
{
    for($r = ""; $n >= 0; $n = intval($n / 26) - 1)
        $r = chr($n%26 + 0x41) . $r;
    return $r;
}

推薦答案

這是一個很好的簡單遞歸函數(shù)(基于零索引數(shù),意思是 0 == A、1 == B 等)...

Here's a nice simple recursive function (Based on zero indexed numbers, meaning 0 == A, 1 == B, etc)...

function getNameFromNumber($num) {
    $numeric = $num % 26;
    $letter = chr(65 + $numeric);
    $num2 = intval($num / 26);
    if ($num2 > 0) {
        return getNameFromNumber($num2 - 1) . $letter;
    } else {
        return $letter;
    }
}

如果你想要一個索引(1 == A,等等):

And if you want it one indexed (1 == A, etc):

function getNameFromNumber($num) {
    $numeric = ($num - 1) % 26;
    $letter = chr(65 + $numeric);
    $num2 = intval(($num - 1) / 26);
    if ($num2 > 0) {
        return getNameFromNumber($num2) . $letter;
    } else {
        return $letter;
    }
}

使用從 0 到 10000 的數(shù)字進行測試...

Tested with numbers from 0 to 10000...

這篇關(guān)于獲取數(shù)字的類似excel的列名的算法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設(shè)置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數(shù)組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產(chǎn)生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 日韩国产一区 | 在线观看中文字幕 | 神马久久春色视频 | 日韩波多野结衣 | 91在线看| 四色永久 | 国产精品毛片无码 | 天天操伊人 | 中文在线a在线 | 黄色91在线| 中文二区 | 超碰精品在线 | 一区二区三区四区国产 | 日本不卡免费新一二三区 | 欧美日韩看片 | 高清欧美性猛交xxxx黑人猛交 | 免费能直接在线观看黄的视频 | 97人人爱 | 国产精品区一区二 | 另类一区 | 国产高清视频一区二区 | 国产91丝袜在线播放 | 亚洲女人的天堂 | 97久久精品午夜一区二区 | 亚洲精品在线看 | 日本视频中文字幕 | 亚洲成人一区 | 福利片一区二区 | 久久99精品久久久久蜜桃tv | 日韩精品在线免费观看 | 国产一区二区三区久久久久久久久 | 在线观看av不卡 | 亚洲欧美综合精品久久成人 | 国产美女h视频 | 二区三区视频 | 日本午夜精品 | 日韩欧美专区 | 成人亚洲| 日韩欧美一区二区三区在线播放 | 日本福利在线观看 | 2019天天操 |