php 資金格式轉換函式

來源:互聯網
上載者:User
 代碼如下 複製代碼

 

// 函數名:ExchangeMoney($N_money)

// 作 用:資金轉換函式

// 參 數:$N_money(待轉換的金額數字)

// 傳回值:字串

// 備 註:本函數樣本:$char=ExchangeMoney(5645132.3155) ==> $char='¥5,645,132.31'

//-----------------------------------------------------------------------------------

 

function ExchangeMoney($N_money)

{

$A_tmp=explode(".",$N_money ); //將數字按小數點分成兩部分,並存入數組$A_tmp

$I_len=strlen($A_tmp[0]); //測出小數點前面位元的寬度

if($I_len%3==0)

{

$I_step=$I_len/3; //如前面位元的寬度mod 3 = 0 ,可按,分成$I_step 部分

}else

{

$step=($len-$len%3)/3+1; //如前面位元的寬度mod 3 != 0 ,可按,分成$I_step 部分+1

}

 

 

$C_cur="";

//對小數點以前的金額數字進行轉換

while($I_len<>0)

{

$I_step--;

 

 

if($I_step==0)

{

$C_cur .= substr($A_tmp[0],0,$I_len-($I_step)*3);

}else

{

$C_cur .= substr($A_tmp[0],0,$I_len-($I_step)*3).",";

}

 

 

$A_tmp[0]=substr($A_tmp[0],$I_len-($I_step)*3);

$I_len=strlen($A_tmp[0]);

}

 

 

//對小數點後面的金額的進行轉換

if($A_tmp[1]=="")

{

$C_cur .= ".00";

}else

{

$I_len=strlen($A_tmp[1]);

if($I_len&lt;2)

{

$C_cur .= ".".$A_tmp[1]."0";

}else

{

$C_cur .= ".".substr($A_tmp[1],0,2);

}

}

//加上人民幣符號並傳出

$C_cur="¥".$C_cur;

return $C_cur;

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.