搜尋
首頁後端開發php教程将数字格式的计算结果转为汉字格式

有没有想过将数字格式的计算结果转化为汉字格式的? 有人会问"干嘛要转, 数字形式不是蛮好嘛", 可是当这个数字很长的时候就不太容易读出来了吧, 就算是有千分位的分隔符也不易顺口说出, 因为这个符号是位英语行方便的, 不是适合我们的读法. 那就自己写一个函数来完成这项任务吧.

将下列代码加到你的网页里, 通过num2chi()函数的调用就可以实现上述功能了, 快来试一试吧. 为了各位能读懂这段代码, 在下特意加入详细注解, 还请老鸟们不要嫌罗唆, ^_^.

//----------------------FUNCTION BEGIN-----------------------------
//-------------------------------------------
//函数名: num2chi()
//叁 数: 一数字
//返回值: 一字符串
//功 能: 将难读的长串数字转为顺口读出的汉字
//作 者: chen.anson 
//站 点: HTTP://dreamer.oso.com.cn
//-------------------------------------------

function num2chi(result) {

var chiresult = "";      //定义返回值叁数chiresult为字符形式
result = result.toString();  //将result转为字符形式
result = result.toLowerCase();
resultlen = result.length;  //定义resultlen为result的长度
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(".","点");
  tempresult = tempresult.replace("e+","幂");
}

while(tempresult.indexOf("零零")!=-1)  //避免字符串tempresult中出现"零零", 但又不能改变字符串长度
{
  tempresult = tempresult.replace("零零","位零");
}

resultlen = tempresult.length;  //再次确认tempresult的长度, 因"e+"->"幂"会引起长度变化

for (i=1,j=1,k=1;i {
  //防止尾数为零, 如八拾零, 二拾零万
  if (tempresult.charAt(resultlen-1)=="零"&&i==1)
    chiresult = "位";
  else if (tempresult.charAt(resultlen-i)=="零"&&j==1)
    chiresult = "位" + chiresult;
  //--------------------------------

  //避免把"幂"和"点"当做实际位数, 而且单位确认变量重新计数
  else if (tempresult.charAt(resultlen-i)=="幂")
  {
    j=1;k=1;chiresult = tempresult.charAt(resultlen-i) + chiresult;continue;
  }
  else if (tempresult.charAt(resultlen-i)=="点")
  {
    j=1;k=1;chiresult = tempresult.charAt(resultlen-i) + chiresult;continue;
  }
  //--------------------------------------
  else
  chiresult = tempresult.charAt(resultlen-i) + chiresult;
  //添加数字单位
    if (tempresult.charAt(resultlen-i-1)!="位"&&tempresult.charAt(resultlen-i-1)!="零"&&tempresult.charAt(resultlen-i-1)!="幂")
    {
      if (j==1&&i       else if (j==2&&i       else if (j==3&&i     }
    if (j==4&&i     if (k==4&&i     else if (k==8&&i   //-----------
  j++;k++;
}

while(chiresult.indexOf("位")!=-1)  //避免字符串chiresult中出现"位"
{
  chiresult = chiresult.replace("位","");
}

if (chiresult.substr(0,2)=="一拾")  //避免出现"一拾二"等情况
chiresult = chiresult.substring(1,chiresult.length);

//幂和小数点後的数字应直接读出, 而没有单位
if (chiresult.search("幂")>=0&&chiresult.search("点")>=0)
{
  rebegin = chiresult.substring(0,chiresult.indexOf("点"));
  relast = chiresult.substring(chiresult.indexOf("幂"),chiresult.length);
  remid = chiresult.substring(chiresult.indexOf("点"),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("点"));
  relast = chiresult.substring(chiresult.indexOf("点"),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块中试运行一下看看结果对不对, 另外可以访问我的主页http://dreamer.oso.com.cn 在休闲广场里有一个彩票页面, 就是用这段代码实现的, 欢迎光临.
hi='4648000567542450084.16415846E+766600050';
document.write(hi+"
"+num2chi(hi));
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP和Python:解釋了不同的範例PHP和Python:解釋了不同的範例Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP和Python:深入了解他們的歷史PHP和Python:深入了解他們的歷史Apr 18, 2025 am 12:25 AM

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

在PHP和Python之間進行選擇:指南在PHP和Python之間進行選擇:指南Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP和框架:現代化語言PHP和框架:現代化語言Apr 18, 2025 am 12:14 AM

PHP在現代化進程中仍然重要,因為它支持大量網站和應用,並通過框架適應開發需求。 1.PHP7提升了性能並引入了新功能。 2.現代框架如Laravel、Symfony和CodeIgniter簡化開發,提高代碼質量。 3.性能優化和最佳實踐進一步提升應用效率。

PHP的影響:網絡開發及以後PHP的影響:網絡開發及以後Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型?PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型?Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP如何處理對象克隆(克隆關鍵字)和__clone魔法方法?PHP如何處理對象克隆(克隆關鍵字)和__clone魔法方法?Apr 17, 2025 am 12:24 AM

PHP中使用clone關鍵字創建對象副本,並通過\_\_clone魔法方法定制克隆行為。 1.使用clone關鍵字進行淺拷貝,克隆對象的屬性但不克隆對象屬性內的對象。 2.通過\_\_clone方法可以深拷貝嵌套對象,避免淺拷貝問題。 3.注意避免克隆中的循環引用和性能問題,優化克隆操作以提高效率。

PHP與Python:用例和應用程序PHP與Python:用例和應用程序Apr 17, 2025 am 12:23 AM

PHP適用於Web開發和內容管理系統,Python適合數據科學、機器學習和自動化腳本。 1.PHP在構建快速、可擴展的網站和應用程序方面表現出色,常用於WordPress等CMS。 2.Python在數據科學和機器學習領域表現卓越,擁有豐富的庫如NumPy和TensorFlow。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。