PHP Money format conversion Function _php Tutorial

Source: Internet
Author: User
This paper introduces a self-defined money conversion function, which can be converted into bank format funds according to the information entered by the user, which needs to be referenced by students.
The code is as follows Copy Code

Function name: Exchangemoney ($N _money)

Role: money transfer function

Parameters: $N _money (number of amounts to be converted)

return value: String

Note: Example of this function: $char =exchangemoney (5645132.3155) ==> $char = ' ¥5,645,132.31 '

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

function Exchangemoney ($N _money)

{

$A _tmp=explode (".", $N _money); Divides a number into two parts by a decimal point and deposits it into an array $a_tmp

$I _len=strlen ($A _tmp[0]); Measure the width of the number of digits before the decimal point

if ($I _len%3==0)

{

$I _step= $I _LEN/3; such as the width of the preceding digit mod 3 = 0, can be pressed, divided into the $i_step section

}else

{

$step = ($len-$len%3)/3+1; As the front digit width mod 3! = 0, can be pressed, divided into $i_step part +1

}

$C _cur= "";

Convert amount numbers before decimal point

while ($I _len<>0)

{

$I _step--;

if ($I _step==0)

{

$C _cur. = substr ($A _tmp[0],0, $I _len-($I _step));

}else

{

$C _cur. = substr ($A _tmp[0],0, $I _len-($I _step)). ",";

}

$A _tmp[0]=substr ($A _tmp[0], $I _len-($I _step);

$I _len=strlen ($A _tmp[0]);

}

Convert the amount after the decimal point

if ($A _tmp[1]== "")

{

$C _cur. = ". 00";

}else

{

$I _len=strlen ($A _tmp[1]);

if ($I _len<2)

{

$C _cur. = ".". $A _tmp[1]. " 0 ";

}else

{

$C _cur. = ".". substr ($A _tmp[1],0,2);

}

}

Plus the renminbi sign and out

$C _cur= "¥". $C _cur;

return $C _cur;

}

http://www.bkjia.com/PHPjc/631665.html www.bkjia.com true http://www.bkjia.com/PHPjc/631665.html techarticle This paper introduces a self-defined money conversion function, which can be converted into bank format funds according to the information entered by the user, which needs to be referenced by students. Code to copy code as follows ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.