將數(shù)字格式的計(jì)算結(jié)果轉(zhuǎn)為漢字格式
Jun 01, 2016 pm 02:29 PM將下列代碼加到你的網(wǎng)頁里, 通過num2chi()函數(shù)的調(diào)用就可以實(shí)現(xiàn)上述功能了, 快來試一試吧. 為了各位能讀懂這段代碼, 在下特意加入詳細(xì)注解, 還請(qǐng)老鳥們不要嫌羅唆, ^_^.
//----------------------FUNCTION BEGIN-----------------------------
//-------------------------------------------
//函數(shù)名: num2chi()
//叁 數(shù): 一數(shù)字
//返回值: 一字符串
//功 能: 將難讀的長(zhǎng)串?dāng)?shù)字轉(zhuǎn)為順口讀出的漢字
//作 者: chen.anson
//站 點(diǎn): HTTP://dreamer.oso.com.cn
//-------------------------------------------
function num2chi(result) {
var chiresult = ""; //定義返回值叁數(shù)chiresult為字符形式
result = result.toString(); //將result轉(zhuǎn)為字符形式
result = result.toLowerCase();
resultlen = result.length; //定義resultlen為result的長(zhǎng)度
temPResult = result; //定義中間變量tempresult
for (i=1;i {
tempresult = tempresult.replace("1","一");
tempresult = tempresult.replace("2","二");
tempresult = tempresult.replace("3","三");
tempresult = tempresult.replace("4","四");
tempresult = tempresult.replace("5","五");
tempresult = tempresult.replace("6","六");
tempresult = tempresult.replace("7","七");
tempresult = tempresult.replace("8","八");
tempresult = tempresult.replace("9","九");
tempresult = tempresult.replace("0","零");
tempresult = tempresult.replace(".","點(diǎn)");
tempresult = tempresult.replace("e+","冪");
}
while(tempresult.indexOf("零零")!=-1) //避免字符串tempresult中出現(xiàn)"零零", 但又不能改變字符串長(zhǎng)度
{
tempresult = tempresult.replace("零零","位零");
}
resultlen = tempresult.length; //再次確認(rèn)tempresult的長(zhǎng)度, 因"e+"->"冪"會(huì)引起長(zhǎng)度變化
for (i=1,j=1,k=1;i {
//防止尾數(shù)為零, 如八拾零, 二拾零萬
if (tempresult.charAt(resultlen-1)=="零"&&i==1)
chiresult = "位";
else if (tempresult.charAt(resultlen-i)=="零"&&j==1)
chiresult = "位" + chiresult;
//--------------------------------
//避免把"冪"和"點(diǎn)"當(dāng)做實(shí)際位數(shù), 而且單位確認(rèn)變量重新計(jì)數(shù)
else if (tempresult.charAt(resultlen-i)=="冪")
{
j=1;k=1;chiresult = tempresult.charAt(resultlen-i) + chiresult;continue;
}
else if (tempresult.charAt(resultlen-i)=="點(diǎn)")
{
j=1;k=1;chiresult = tempresult.charAt(resultlen-i) + chiresult;continue;
}
//--------------------------------------
else
chiresult = tempresult.charAt(resultlen-i) + chiresult;
//添加數(shù)字單位
if (tempresult.charAt(resultlen-i-1)!="位"&&tempresult.charAt(resultlen-i-1)!="零"&&tempresult.charAt(resultlen-i-1)!="冪")
{
if (j==1&&i
if (j==4&&i
j++;k++;
}
while(chiresult.indexOf("位")!=-1) //避免字符串chiresult中出現(xiàn)"位"
{
chiresult = chiresult.replace("位","");
}
if (chiresult.substr(0,2)=="一拾") //避免出現(xiàn)"一拾二"等情況
chiresult = chiresult.substring(1,chiresult.length);
//冪和小數(shù)點(diǎn)後的數(shù)字應(yīng)直接讀出, 而沒有單位
if (chiresult.search("冪")>=0&&chiresult.search("點(diǎn)")>=0)
{
rebegin = chiresult.substring(0,chiresult.indexOf("點(diǎn)"));
relast = chiresult.substring(chiresult.indexOf("冪"),chiresult.length);
remid = chiresult.substring(chiresult.indexOf("點(diǎn)"),chiresult.indexOf("冪"));
for (i=1;i {
remid = remid.replace("拾","");
remid = remid.replace("百","");
remid = remid.replace("千","");
remid = remid.replace("萬","");
remid = remid.replace("億","");
}
chiresult = rebegin + remid + relast;
}
else if (chiresult.search("冪")=0)
{
rebegin = chiresult.substring(0,chiresult.indexOf("點(diǎn)"));
relast = chiresult.substring(chiresult.indexOf("點(diǎn)"),chiresult.length);
for (i=1;i {
relast = relast.replace("拾","");
relast = relast.replace("百","");
relast = relast.replace("千","");
relast = relast.replace("萬","");
relast = relast.replace("億","");
}
chiresult = rebegin + relast;
}
if (chiresult.search("冪")>=0) //將"冪"替換為"乘以拾的", 這樣可以直接讀出
{
chiresult = chiresult.replace("冪","乘以拾的");
chiresult = chiresult + "次方";
}
return chiresult;
}
//----------------------FUNCTION END-------------------------------
將下面這兩條語句放到script塊中試運(yùn)行一下看看結(jié)果對(duì)不對(duì), 另外可以訪問我的主頁http://dreamer.oso.com.cn 在休閑廣場(chǎng)里有一個(gè)彩票頁面, 就是用這段代碼實(shí)現(xiàn)的, 歡迎光臨.
hi='4648000567542450084.16415846E+766600050';
document.write(hi+"
"+num2chi(hi));

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez
