Rumah > php教程 > php手册 > php 格式化金额,php金额

php 格式化金额,php金额

WBOY
Lepaskan: 2016-06-13 09:15:57
asal
1309 orang telah melayarinya

php 格式化金额,php金额

<span> 1</span> <span>/*</span><span>*
</span><span> 2</span> <span> * 格式化金额
</span><span> 3</span> <span> *
</span><span> 4</span> <span> * @param int $money
</span><span> 5</span> <span> * @param int $len
</span><span> 6</span> <span> * @param string $sign
</span><span> 7</span> <span> * @return string
</span><span> 8</span>  <span>*/</span>
<span> 9</span> <span>function</span> format_money(<span>$money</span>, <span>$len</span>=2, <span>$sign</span>='¥'<span>){
</span><span>10</span>     <span>$negative</span> = <span>$money</span> > 0 ? '' : '-'<span>;
</span><span>11</span>     <span>$int_money</span> = <span>intval</span>(<span>abs</span>(<span>$money</span><span>));
</span><span>12</span>     <span>$len</span> = <span>intval</span>(<span>abs</span>(<span>$len</span><span>));
</span><span>13</span>     <span>$decimal</span> = '';<span>//</span><span>小数</span>
<span>14</span>     <span>if</span> (<span>$len</span> > 0<span>) {
</span><span>15</span>         <span>$decimal</span> = '.'.<span>substr</span>(<span>sprintf</span>('%01.'.<span>$len</span>.'f', <span>$money</span>),-<span>$len</span><span>);
</span><span>16</span> <span>    }
</span><span>17</span>     <span>$tmp_money</span> = <span>strrev</span>(<span>$int_money</span><span>);
</span><span>18</span>     <span>$strlen</span> = <span>strlen</span>(<span>$tmp_money</span><span>);
</span><span>19</span>     <span>for</span> (<span>$i</span> = 3; <span>$i</span> < <span>$strlen</span>; <span>$i</span> += 3<span>) {
</span><span>20</span>         <span>$format_money</span> .= <span>substr</span>(<span>$tmp_money</span>,0,3).','<span>;
</span><span>21</span>         <span>$tmp_money</span> = <span>substr</span>(<span>$tmp_money</span>,3<span>);
</span><span>22</span> <span>    }
</span><span>23</span>     <span>$format_money</span> .= <span>$tmp_money</span><span>;
</span><span>24</span>     <span>$format_money</span> = <span>strrev</span>(<span>$format_money</span><span>);
</span><span>25</span>     <span>return</span> <span>$sign</span>.<span>$negative</span>.<span>$format_money</span>.<span>$decimal</span><span>;
</span><span>26</span> }
Salin selepas log masuk

 

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan