萬內整數數字轉換大寫漢字 vba實現

2021-10-05 12:24:05 字數 2558 閱讀 5275

萬內整數數字轉換大寫漢字

vba實現

先上**

sub test()

dim a(1 to 10) as string '大寫漢字陣列

dim tmpstr as string '提取原單元格字串

dim count(1 to 4) as string '單位陣列

dim num as integer '統計當前單位

dim time as integer '統計當前零進行判斷

a(1) = "零"

a(2) = "壹"

a(3) = "貳"

a(4) = "叄"

a(5) = "肆"

a(6) = "伍"

a(7) = "陸"

a(8) = "柒"

a(9) = "捌"

a(10) = "玖"

count(1) = "圓整"

count(2) = "拾"

count(3) = "佰"

count(4) = "仟"

length = ubound(a) - lbound(a) + 1 '獲取長度

time = 0

newvaluestr = ""

orivaluestr = trim(str(activecell.value)) '去除頭尾部空格,

activecell.offset(0, 1).select '將當前單元格移到右邊

num = len(orivaluestr) '獲取總長

for i = 1 to len(orivaluestr) '字串從左往右進行判斷

tmpstr = mid(orivaluestr, i, 1)

'msgbox (tmpstr)

if tmpstr = 0 then '判斷特殊情況0

if time = 0 then 'time這裡負責判斷0分支

newvaluestr = newvaluestr & a(tmpstr + 1)

time = 1

end if

num = num - 1

else

if tmpstr = 1 and i = len(orivaluestr) - 1 and len(orivaluestr) = 2 then '判斷倆位數時十位數是否為1進行處理

newvaluestr = newvaluestr & count(num)

num = num - 1

else

newvaluestr = newvaluestr & a(tmpstr + 1) & count(num)

num = num - 1

time = 0

end if

end if

next i

if time = 1 then '如果個位及前幾位都為零,做特殊判斷

if len(newvaluestr) = 1 then

newvaluestr = newvaluestr & count(1)

else

newvaluestr = left(newvaluestr, len(newvaluestr) - 1)

newvaluestr = newvaluestr & count(1)

end if

end if

activecell.value = newvaluestr '結果返回,結束

end sub

output:

xlsm

9玖圓整

0零圓整

1壹圓整

123壹佰貳拾叄圓整

102壹佰零貳圓整

311叄佰壹拾壹圓整

1234

壹仟貳佰叄拾肆圓整

1023

壹仟零貳拾叄圓整

1002

壹仟零貳圓整

2001

貳仟零壹圓整

3000

叄仟圓整

130壹佰叄拾圓整

31叁拾壹圓整

3001

叄仟零壹圓整

3100

叄仟壹佰圓整

9876

玖仟捌佰柒拾陸圓整

12拾貳圓整

111壹佰壹拾壹圓整

10拾圓整

11拾壹圓整

21貳拾壹圓整

101壹佰零壹圓整

110壹佰壹拾圓整

3000

叄仟圓整

200貳佰圓整

C 數字 轉換 大寫漢字

呼叫方法 double m try catch formatexception ex public class money public money double m 判斷輸入的數字是否大於double型別 private bool isnumber 數字轉換成大寫漢字主函式 返回轉換後的大寫漢字 ...

C 數字轉換成大寫漢字程式

using system using system.collections.generic using system.text private double j private string numchinesecharacter new string public money double m 判...

將數字金錢轉換對應的大寫漢字金錢

將數字金額轉換成漢字大寫金額 待轉金額 返回大寫人民幣 public static string getchinesemoneybymoneys decimal number var res regex.replace dou,m 負元空零壹貳叄肆伍陸柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰 m...