Convert the result of numerical format into Chinese character format _php Foundation

Source: Internet
Author: User
Have you ever thought of converting the results of numerical formats into Chinese characters? Someone would ask, "Why do you turn? The digital form is not very good", but when this number is very long time is not too easy to read out of it, even if there are thousands of separators are not easy to speak out, because this symbol is an English line convenient, not suitable for our reading method. Write a function to do the task yourself.

Add the following code to your Web page, using the Num2chi () function call to achieve the above function, come and try it. In order to you can read this code, in the next deliberately add detailed annotations, but also please the old birds do not bother, ^_^.

----------------------FUNCTION BEGIN-----------------------------
//-------------------------------------------
Function name: Num2chi ()
Three numbers: a number
Return value: A string
Function: Turn difficult to read the long string of numbers into a readable character
Author: Chen.anson
Site: HTTP://dreamer.oso.com.cn
//-------------------------------------------

function Num2chi (Result) {

var chiresult = ""; Defines the return value of three Chiresult as a character form
result = Result.tostring (); Convert result to character form
result = Result.tolowercase ();
Resultlen = Result.length; Defines the length of Resultlen as result
Tempresult = result; Define Intermediate variable Tempresult

for (i=1;i<=resultlen;i++)///Replace all digits in string tempresult with Chinese characters
{
Tempresult = Tempresult.replace ("1", "one");
Tempresult = Tempresult.replace ("2", "two");
Tempresult = Tempresult.replace ("3", "three");
Tempresult = Tempresult.replace ("4", "four");
Tempresult = Tempresult.replace ("5", "Five");
Tempresult = Tempresult.replace ("6", "Six");
Tempresult = Tempresult.replace ("7", "Seven");
Tempresult = Tempresult.replace ("8", "Eight");
Tempresult = Tempresult.replace ("9", "Nine");
Tempresult = Tempresult.replace ("0", "0");
Tempresult = Tempresult.replace (".", "point");
Tempresult = Tempresult.replace ("e+", "Power");
}

while (Tempresult.indexof ("00")!=-1)//Avoid "00" in string tempresult, but cannot change string length
{
Tempresult = Tempresult.replace ("00", "bit 0");
}

Resultlen = Tempresult.length; Re-confirm the length of the Tempresult, because "e+"-> "power" will cause length changes

for (i=1,j=1,k=1;i<=resultlen;i++)//Start conversion, I is the number of digits to confirm three, J is "Chichong" to confirm three, K is "trillion" to confirm three numbers
{
Prevent the mantissa from being zero, such as eight titbits, two picks 0
if (Tempresult.charat (resultlen-1) = = "0" &&i==1)
Chiresult = "bit";
else if (Tempresult.charat (resultlen-i) = = "0" &&j==1)
Chiresult = "bit" + chiresult;
//--------------------------------

Avoid the "power" and "point" as the actual digits, and the unit confirm variable to be counted again
else if (Tempresult.charat (resultlen-i) = = "Power")
{
J=1;k=1;chiresult = Tempresult.charat (resultlen-i) + chiresult;continue;
}
else if (Tempresult.charat (resultlen-i) = = "point")
{
J=1;k=1;chiresult = Tempresult.charat (resultlen-i) + chiresult;continue;
}
//--------------------------------------
Else
Chiresult = Tempresult.charat (resultlen-i) + Chiresult;
Add a number unit
if (Tempresult.charat (resultlen-i-1)!= "bit" &&tempresult.charat (resultlen-i-1)!= "0" && Tempresult.charat (resultlen-i-1)!= "power")
{
if (j==1&&i<resultlen) Chiresult = "Pick up" + chiresult;
else if (j==2&&i<resultlen) Chiresult = "hundred" + Chiresult;
else if (j==3&&i<resultlen) Chiresult = "thousand" + Chiresult;
}
if (J==4&&i<resultlen) j=0;
if (K==4&&i<resultlen&&tempresult.charat (resultlen-i-1)!= "power") Chiresult = "million" + Chiresult;
else if (K==8&&i<resultlen&&tempresult.charat (resultlen-i-1)!= "power") {K=0;chiresult = "billion" + Chiresult;}
//-----------
j++;k++;
}

while (Chiresult.indexof ("bit")!=-1)//avoid "bit" in string Chiresult
{
Chiresult = Chiresult.replace ("Bit", "");
}

if (chiresult.substr (0,2) = "one pick Up")//Avoid "one to two" and so on
Chiresult = chiresult.substring (1,chiresult.length);

The numbers after the power and the decimal point should be read directly, without the unit
if (Chiresult.search ("Power") >=0&&chiresult.search ("point") >=0)
{
Rebegin = chiresult.substring (0,chiresult.indexof ("point"));
Relast = chiresult.substring (Chiresult.indexof ("Power"), chiresult.length);
Remid = chiresult.substring (Chiresult.indexof ("point"), Chiresult.indexof ("Power"));
for (i=1;i<=remid.length;i++)
{
Remid = Remid.replace ("Pick", "");
Remid = Remid.replace ("Hundred", "");
Remid = Remid.replace ("Thousand", "");
Remid = Remid.replace ("Million", "");
Remid = Remid.replace ("Billion", "");
}
Chiresult = rebegin + Remid + relast;
}
else if (Chiresult.search ("Power") <0&&chiresult.search ("point") >=0)
{
Rebegin = chiresult.substring (0,chiresult.indexof ("point"));
Relast = chiresult.substring (Chiresult.indexof ("point"), chiresult.length);
for (i=1;i<=relast.length;i++)
{
Relast = Relast.replace ("Pick", "");
Relast = Relast.replace ("Hundred", "");
Relast = Relast.replace ("Thousand", "");
Relast = Relast.replace ("Million", "");
Relast = Relast.replace ("Billion", "");
}
Chiresult = Rebegin + relast;
}

if (Chiresult.search ("power") >=0)//Replace "power" with "multiplied" so that you can read directly
{
Chiresult = Chiresult.replace ("Power", "multiplied by the pick");
Chiresult = Chiresult + "Second Party";
}
return chiresult;
}

----------------------FUNCTION End-------------------------------


Put the following two statements into the script block to see if the results are correct, and to visit my homepage http://dreamer.oso.com.cn in the Leisure Square has a lottery page, is to use this code to achieve, welcome.
Hi= ' 4648000567542450084.16415846E+766600050 ';
document.write (hi+ "<br>" +num2chi (HI));

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.