這篇文章主要介紹“php如何實現(xiàn)數(shù)值轉(zhuǎn)十進(jìn)制”,在日常操作中,相信很多人在php如何實現(xiàn)數(shù)值轉(zhuǎn)十進(jìn)制問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”php如何實現(xiàn)數(shù)值轉(zhuǎn)十進(jìn)制”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)一直在為企業(yè)提供服務(wù),多年的磨煉,使我們在創(chuàng)意設(shè)計,營銷型網(wǎng)站到技術(shù)研發(fā)擁有了開發(fā)經(jīng)驗。我們擅長傾聽企業(yè)需求,挖掘用戶對產(chǎn)品需求服務(wù)價值,為企業(yè)制作有用的創(chuàng)意設(shè)計體驗。核心團(tuán)隊擁有超過十年以上行業(yè)經(jīng)驗,涵蓋創(chuàng)意,策化,開發(fā)等專業(yè)領(lǐng)域,公司涉及領(lǐng)域有基礎(chǔ)互聯(lián)網(wǎng)服務(wù)成都聯(lián)通服務(wù)器托管、成都app軟件開發(fā)、手機(jī)移動建站、網(wǎng)頁設(shè)計、網(wǎng)絡(luò)整合營銷。
php實現(xiàn)數(shù)值轉(zhuǎn)十進(jìn)制的方法:1、創(chuàng)建一個PHP示例文件;2、通過“function show_convert_to_base_10 ($number, $base){...}”方法將任意進(jìn)制的數(shù)轉(zhuǎn)換成十進(jìn)制即可。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php怎么實現(xiàn)數(shù)值轉(zhuǎn)十進(jìn)制?
php實現(xiàn)將任意進(jìn)制數(shù)轉(zhuǎn)換成10進(jìn)制的方法
具體如下:
php將任意進(jìn)制的數(shù)轉(zhuǎn)換成10進(jìn)制,例如8進(jìn)制轉(zhuǎn)換成10進(jìn)制,16進(jìn)制轉(zhuǎn)換成10進(jìn)制
<?php # Show the steps involved in converting a number # from any base (like octal or hex) to base 10 # See below for examples, instructions and copyright function show_convert_to_base_10 ($number, $base) { // If the number contains a decimal component if (strstr ($number, '.')) { // Get the integer and decimal components list ($integer, $decimal) = explode ('.', $number); } else { // The number is an integer $integer = $number; } print "<b>Convert the base $base number $number to a base 10 number:</b><blockquote>"; print "Convert the integer component ($integer) of the number:<blockquote>"; // Compute the value of the integer component // Loop through the integer digit by digit // Reverse the number for easier handling $integer = strrev ($integer); $length = strlen ($integer); for ($pos = 0; $pos < $length; ++$pos) { /* PHP lets you treat strings and numbers like arrays Specify an offset and get the character at that position */ $digit = $integer[$pos]; // Handle character values for digits // (for bases greater than 10) if (eregi ('[a-z]', $digit)) { $digit_value = (ord (strtolower ($digit)) - ord ('a')) + 10; $digit = "$digit ($digit_value)"; } else { $digit_value = $digit; } // Multiply the current digit by the radix // raised to the power of the current position $result = $digit_value * pow ($base, $pos); print "Multiply the value of the digit at position $pos by the value of the radix ($base) raised to the power of the position ($pos):<br/>"; print "$digit * $base<sup>$pos</sup> = $result <br/><br/>"; $sums[] = $result; } print '</blockquote>'; if (isset ($decimal)) { print "Convert the decimal component (0.$decimal) of the number:<blockquote>"; // Pad the number with a leading 0 so that we can // start at position 1 $decimal = '0'.$decimal; $length = strlen ($decimal); for ($pos = 1; $pos < $length; ++$pos) { $digit = $decimal[$pos]; // Handle character values for digits // (for bases greater than 10) if (eregi ('[a-z]', $digit)) { $digit_value = (ord (strtolower ($digit)) - ord ('a')) + 10; $digit = "$digit ($digit_value)"; } else { $digit_value = $digit; } // Multiply the current digit by the radix // raised to the power of the current position $result = $digit_value * pow (1/$base, $pos); print "Multiply the value of the digit at position $pos by the value of the 1/radix ($base) raised to the power of the position ($pos):<br/>"; print "$digit * 1/$base<sup>$pos</sup> = $result<br/><br/>"; $sums[] = $result; } print '</blockquote>'; } $sums = implode (' + ', $sums); eval ("\$base_10_value = $sums;"); print "</blockquote>The value of the base $base number $number in base 10 is $base_10_value. <br/>"; print "This number is derived from the sum of the values of the previous operations ($sums). <br/> <br/>"; }
到此,關(guān)于“php如何實現(xiàn)數(shù)值轉(zhuǎn)十進(jìn)制”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
當(dāng)前名稱:php如何實現(xiàn)數(shù)值轉(zhuǎn)十進(jìn)制
分享網(wǎng)址:http://muchs.cn/article46/jsoihg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、小程序開發(fā)、手機(jī)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、企業(yè)建站、響應(yīng)式網(wǎng)站
聲明:本網(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)