Have you ever thought about converting the calculation results in numerical format into Chinese character format? Some people will ask, "Why do you need to convert? Isn't the numerical format pretty good?" But when the number is very long, it is not easy to read, right? Even if there is a thousands separator, it is not easy to say it smoothly, because this symbol is convenient for English speakers and is not suitable for our reading. Then write a function yourself to complete this task.
Add the following code to your web page, and the above functions can be achieved by calling the num2chi() function. Come and give it a try. In order for everyone to understand this code, I have specially added detailed comments below. Please also ask Birds, don't be too intrusive, ^_^.
//-------------------------FUNCTION BEGIN------- -----------------------
//---------------------- --------------------
//Function name: num2chi()
//Parameter: one number
//Return value: one String
//Function: Convert a long string of difficult-to-read numbers into easily readable Chinese characters
//Author: chen.anson
//Site: HTTP://dreamer.oso .com.cn
//------------------------------------------------ --
function num2chi(result) {
var chiresult = ""; ; Convert result to character form
result = result.toLowerCase();
resultlen = result.length; //Define resultlen as the length of result
tempresult = result; //Define intermediate variable tempresult
for (i=1;i<=resultlen;i++) //Replace all numbers in the string tempresult with Chinese characters
{
tempresult = tempresult.replace("1","一") ;
tempresult = tempresult.replace("2","二");
tempresult = tempresult.replace("3","三");
tempresult = tempresult.replace("4", "four");
tempresult = tempresult.replace("5","五");
tempresult = tempresult.replace("6","六");
tempresult = tempresult.replace(( "7","七");
tempresult = tempresult.replace("8","八");
tempresult = tempresult.replace("9","九");
tempresult = tempresult.replace("0","zero");
tempresult = tempresult.replace(".","point");
tempresult = tempresult.replace("e+","power");
}
while(tempresult.indexOf("zero-zero")!=-1) //Avoid "zero-zero" appearing in the string tempresult, but cannot change the length of the string
{
tempresult = tempresult.replace("zero zero", "bit zero");
}
resultlen = tempresult.length; //Confirm the length of tempresult again, because "e+"-> "Power" will cause the length to change
for (i=1,j=1,k=1;i<=resultlen;i++) //Start conversion, i is the number of digits to confirm the parameter, j is " "Ten hundred thousand" confirmation parameter, k is "trillion" confirmation parameter
{
//Prevent the mantissa from being zero, such as eight hundred and two million
if (tempresult.charAt(resultlen- 1)=="zero"&&i==1)
chiresult = "bit";
else if (tempresult.charAt(resultlen-i)=="zero"&&j==1)
chiresult = "bit" + chiresult;
//--------------------------------
/ /Avoid treating "power" and "point" as actual digits, and the unit confirmation variable is re-counted
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 numerical unit
if (tempresult.charAt(resultlen-i-1)!="bit"&&tempresult.charAt(resultlen-i-1 )!="zero"&&tempresult.charAt(resultlen-i-1)!="power")
if (j==2&&i
if (j== 4&&i
j++;k++;
}
while(chiresult.indexOf("bit")!=-1) //Avoid "bit" appearing in the string chiresult
{
chiresult = chiresult.replace("bit","");
}
if (chiresult.substr(0,2)=="一十") //Avoid "一十二" etc. Case
chiresult = chiresult.substring(1,chiresult.length);
//Powers and numbers after the decimal point should be read directly without units
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("十","");
remid = remid.replace("hundred","");
remid = remid.replace("Thousand","");
remid = remid.replace("10,000","");
remid = remid.replace("100 million","") ;
}
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("十","");
relast = relast.replace("hundred"," ");
relast = relast.replace("Thousand","");
relast = relast.replace("10,000","");
relast = relast.replace("100 million", "");
}
chiresult = rebegin + relast;
}
if (chiresult.search("power")>=0) //Replace "power" with "Multiply by ten", so you can read it directly
{
chiresult = chiresult.replace("power", "multiply by ten");
chiresult = chiresult + "power";
}
return chiresult;
}
//----------------------FUNCTION END------------------ -------------
Put the following two statements into the script block and try running it to see if the result is correct. You can also visit my homepage http: //dreamer.oso.com.cn There is a lottery page in the Leisure Plaza, which is implemented with this code. Welcome.
hi='4648000567542450084.16415846E+766600050';
document.write(hi+"
"+num2chi(hi));