Home  >  Article  >  Backend Development  >  哪位php大神帮忙写个金币转换函数

哪位php大神帮忙写个金币转换函数

WBOY
WBOYOriginal
2016-06-06 20:12:521136browse

100铜币=1银 100银=1金

想写个根据多少铜币 返回相应等级的钱
比如参数为102135铜币 那就是返回10金21银35铜
比如参数为1544铜币 那就是返回15银44铜
以此类推...

php基础不扎实 不知道怎么写最精简

回复内容:

100铜币=1银 100银=1金

想写个根据多少铜币 返回相应等级的钱
比如参数为102135铜币 那就是返回10金21银35铜
比如参数为1544铜币 那就是返回15银44铜
以此类推...

php基础不扎实 不知道怎么写最精简

<code class="php">/**
 * [format_every 换算进制到指定单位]
 * @param  integer  $number [需要换算数值]
 * @param  integer $ary    [每个单位之间的进制]
 * @param  array   $units  [每个单位的描述]
 * @return [String]          [格式化结果]
 */
function format_every($number,$ary = 100,$units = array('铜', '银', '金')) {
  $format = '';
  $prev = 0;
  for($i = count($units) - 1 ; $i >= 0 ; $i--){
    if($next = floor($number / pow($ary, $i))){
      $format .= $next - $prev * $ary . $units[$i];
    }
    $prev = $next;
  }
  return $format;
}
//简单点的
print_r(format_every(12345678));//1234金56银78铜
//假如1坨=100金,则:
print_r(format_every(12345678,100,array('铜', '银', '金','坨')));//12坨34金56银78铜
//字节换算
print_r(format_every(123456789,1024,array('B', 'KB', 'MB', 'GB', 'TB', 'PB')));//117MB755KB277B
</code>

<code>function exchange($copper)
{
    $gold = (int) ($copper / 10000);
    $silver = (int) ($copper / 100 - $gold * 100);
    $copper = $copper % 100;
    
    return array($gold, $silver, $copper);
}</code>

简单做。从额数最大的金开始处理,先整除,再模除,金银铜依次处理。输出的时候判断再做个判断,代码如下:

<code><?php function level($money){
        $gold   = floor($money / 10000);
        $money  = $money % 10000;

        $sliver = floor($money / 100);
        $cu     = $money % 100;

        $result = "";
        if($gold > 0) {
            $result .= $gold ."金";
        }
        if($sliver > 0) {
            $result .= $sliver ."银";    
        }
        if($cu > 0) {
            $result .= $cu ."铜";    
        }

        return $result;
    }

    //test
    echo level(102135);
    echo "<hr>";
    echo level(1544);
    echo "<hr>";
    echo level(99);
</code>

哪位php大神帮忙写个金币转换函数

<code>function level($money){
    $ag = floor($money /100);
    $cu = $money % 100;
    $golden = floor($ag / 100);
    $ag =   $ag % 100;
    echo "{$golden}金{$ag}银{$cu}铜币";
}</code>

很简单的写了一个。

<code class="ruby">def make_change(money, coins)
  dp = [0]
  path = []
  result = {}

  coins.each_with_index do |coin, index|
    coin.upto(money) do |i|
      if !dp[i - coin].nil? && (dp[i].nil? || dp[i - coin] + 1  {1=>35, 100=>21, 10000=>10}</code>

嗯...不知道计算速度快 还是字符串截取速度快

<code><?php $money = 102135;
convert_money($money);

function convert_money($money) {
    $money = (string)$money;
    $coppers = substr($money,-2) ? substr($money,-2) : 0;
    $silver = substr($money,-4,-2) ? substr($money,-4,-2) : 0;
    $glod = substr($money,0,-4) ? substr($money,0,-4) : 0;

    echo "{$glod}金币{$silver}铜币{$coppers}银币";
}
</code></code>

结果: 10金币21铜币35银币

function change($number){

<code>    static $jin;
    static $yin;
    static $ton;
    if($number >= pow(10,4)){
        $jin = intval($number/pow(10,4));
        change($number -$jin*pow(10,4));
    }else if($number >= pow(10,2)){
        $yin = intval($number/pow(10,2));
        change($number-$yin*pow(10,2));
    }else{
        $ton = $number;
    }

    return array($jin,$yin,$ton);
}

$number = 10110245;
print_r(change($number));</code>

<code>/**
 * @param $coins int 币数
 * @return string 格式化信息
 */
function coinFormatter($coins)
{
    $coins = intval($coins);
    $jin   = intval($coins/10000);
    $yin   = intval($coins/100 - $jin*100);
    $tong = $coins%100;

    $info = '';
    if($jin>0) {
        $info = $jin . "金";
    }
    if($yin>0){
        $info = $info.$yin."银";
    }
    if($tong >0){
        $info = $info.$tong."铜";
    }

    return $info;
}

echo coinFormatter(114523);
echo "\n";
echo coinFormatter(523);
echo "\n";
echo coinFormatter(3);
/*
输出结果:
11金45银23铜
5银23铜
3铜
*/</code>

关注微信公众号php技术大全:phpgod,精彩分享每日不断。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn