將數字轉換成漢字的一段ASP程式

來源:互聯網
上載者:User
程式|漢字|轉換 '--------------------數字向漢字轉換的函數集合  ------------------------
'作者:northsnow   
'日期:20051217
'調用方法:  a=convertNumToStr("1234.5678")
'功能: 將這樣的串  1234.5678 轉換成 一千二百三十四點五六七八


''將一串數字轉成對應的漢字
 function convertNumToStr(pNum)
    dim oNum,rValue
    oNum=pNum:rValue=""
   
    '如果給定的不是合理的數字,則返回空串
    if not CheckPattern(oNum,z_PatNum) then
       ConvertNumToStr=rValue
       exit function
    end if  
   
    '將數字前面無用的0去掉
    set rLjc=new RegExp
    rLjc.Pattern="^0{2,}([^.])"
    oNum=rLjc.Replace(oNum,"$1")
    rLjc.Pattern="^0{2,}(\.)"
    oNum=rLjc.Replace(oNum,"0$1")
   
    '將小數點前後部分分開
    arrNum=split(oNum,".")
    frontNum=arrNum(0)
    backNum=""
    if ubound(arrNum)>0 then backNum=arrNum(1)
   
    '----  轉換小數點前面的數----
    oLen=len(frontNum)
  
    if oLen=1 then   '只有一位
       rValue=convertNumToCC(frontNum)
    elseif oLen=2 then   '只有兩位
       if(mid(frontNum,1,1))<>"1" then  rValue=convertNumToCC(mid(frontNum,1,1))
       rValue=rValue & getDigit(2)
       if(mid(frontNum,2,1))<>"0" then rValue=rValue & convertNumToCC(mid(frontNum,2,1))
    else   '大於兩位的情況
       dim curPos,curNum,hasZero
       hasZero=false  '表明在此前有沒有相串連的零
       for i=1 to oLen
          curPos=oLen-i + 1
          curNum=mid(frontNum,i,1)
          if cint(curNum)=0 then     '當前位元為零
              hasZero=true
              '噹噹前位為萬位或者億位,則進行處理
              if (curPos -1) mod 4=0 and curPos>4 then
     rValue=rValue & getDigit(curPos)
     end if
          else   '當前位元不是零
              if hasZero then
               rValue=rValue & "零"
      hasZero=false
     end if
     rValue=rValue & convertNumToCC(curNum) & getDigit(curPos)
          end if
       next 
    end if
   
    '轉換小數點後面的
    if backNum<>"" then
       strBack=""
       for i=1 to len(backNum)
          strBack=strBack & convertNumToCC(mid(backNum,i,1))
       next
       rValue=rValue & "點" & strBack
    end if
    convertNumToStr=rValue
 end function
 
 
 ''將一個數字轉成對應的漢字
 function convertNumToCC(pNum)
    select case pNum
      case 1:convertNumToCC="一"
      case 2:convertNumToCC="二"
      case 3:convertNumToCC="三"
      case 4:convertNumToCC="四"
      case 5:convertNumToCC="五"
      case 6:convertNumToCC="六"
      case 7:convertNumToCC="七"
      case 8:convertNumToCC="八"
      case 9:convertNumToCC="九"
      case 0:convertNumToCC="零"
    end select
 end function
 
 '根據位元返回對應的漢字
 function getDigit(oDigit)
    if (oDigit+2) mod 4=0 then
        getDigit="十"
    elseif (oDigit +1) mod 4=0 then
        getDigit="百"
    elseif oDigit mod 4=0 then
        getDigit="千"
    elseif (oDigit -1) mod 4=0 then
        if ((oDigit-1)/4) mod 2=0 then
            getDigit="億"
        else
            getDigit="萬"
        end if
    end if
 end function



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.