利用php怎么將阿拉伯?dāng)?shù)字與羅馬數(shù)字進行轉(zhuǎn)換?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
具體如下:
<?php // Function that calculates the roman string to the given number: function dec2roman($f) { // Return false if either $f is not a real number, //$f is bigger than 3999 or $f is lower or equal to 0: if(!is_numeric($f) || $f > 3999 || $f <= 0) return false; // Define the roman figures: $roman = array( 'M' => 1000, 'D' => 500, 'C' => 100, 'L' => 50, 'X' => 10, 'V' => 5, 'I' => 1 ); // Calculate the needed roman figures: foreach($roman as $k => $v) if(($amount[$k] = floor($f / $v)) > 0) $f -= $amount[$k] * $v; // Build the string: $return = ''; foreach($amount as $k => $v) { $return .= $v <= 3 ? str_repeat($k, $v) : $k . $old_k; $old_k = $k; } // Replace some spacial cases and return the string: return str_replace(array('VIV','LXL','DCD'),array('IX','XC','CM'),$return); } // echo dec2romen(1981); // Function to get the decimal value of a roman string: function roman2dec($str = '') { // Return false if not at least one letter is in the string: if(is_numeric($str)) return false; // Define the roman figures: $roman = array( 'M' => 1000, 'D' => 500, 'C' => 100, 'L' => 50, 'X' => 10, 'V' => 5, 'I' => 1 ); // Convert the string to an array of roman values: for($i = 0; $i < strlen($str); $i++) if(isset($roman[strtoupper($str[$i])])) $values[] = $roman[strtoupper($str[$i])]; // Calculate the sum of that array: $sum = 0; while($current = current($values)) { $next = next($values); $next > $current ? $sum += $next - $current + 0 * next($values) : $sum += $current; } // Return the value: return $sum; } // echo roman2dec(IX); ?>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
當(dāng)前標(biāo)題:利用php怎么將阿拉伯?dāng)?shù)字與羅馬數(shù)字進行轉(zhuǎn)換-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://aaarwkj.com/article46/godhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、服務(wù)器托管、外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、做網(wǎng)站、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容