pascal 人民幣大寫轉換

2021-04-27 21:55:11 字數 2262 閱讀 6486

我們在程式設計的過程中,特別是開發和財務相關的應用程式的時候,幾乎都會遇到要將阿拉伯數字(一般是貨幣金額)轉換為中文大寫的要求。也有一些轉換程式,但大都不符合財務實際要求,比如最簡單的:

function xd(xx:currency):string;

var dx,ws:string;

i,cd:integer;

int:currency;

begin

int:=trunc((abs(xx)+0.005)*100);

cd:=length(currtostr(int));

dx:='零壹貳叄肆伍陸柒捌玖';

ws:='分角元拾佰仟萬拾佰仟億拾佰仟';

result:= ' ';

i:=1;

while i<=cd do

begin

result:=result+copy(dx,strtoint(copy(currtostr(int),i,1))*2+1,2);

result:=result+copy(ws,(cd-i)*2+1,2);

i:=i+1;

endend

在這裡輸入xd(1234567.89),返回「壹佰貳拾叄萬肆仟伍佰陸拾柒元捌角玖分」,但它並不完美,例如xd(100),返回的卻是:壹佰零拾零元零角零分(應是壹佰元整),顯然這不符合財務工作的實際要求。

中文大寫的特殊性關鍵在對「0」的處理上,我們程式設計的思路也是如此,程式中遇到0的時候並不能簡單地用大寫「零」來代替,要進行如下判斷:是否是第乙個零(即它的左邊是否不為零)?如是第乙個零,還要判斷,它的後面是否全為零(這要分段判斷:億以上,萬以上億以下,元以上萬以下,分以上元以下)?如不是,才能以大寫零來代替;如果是就直接寫上位數如萬(例如:100000,就應是壹拾萬,而不是壹拾零萬)。另一方面如果它的後面不全為零,卻有連續的零,也要注意(例如:1001,就應是壹仟零壹元,而不是壹仟零零壹元)。

以下就是根據這一思路給出的程式(此程式最大可轉換至千億位,在實際工作中也應該足夠了):

……while i<=cd do

begin

if copy(currtostr(int),i,1)<> '0' then

begin

result:=result+copy(dx,strtoint(copy(currtostr(int),i,1))*2+1,2);

result:=result+copy(ws,(cd-i)*2+1,2);

ling:=false;

i:=i+1;

endelse if ling=false and (copy(currtostr(int),i,1)= '0' ) then

begin

if cd-i+1>10 then

begin

w:=0;

for q:=11 to cd-i+1 do

begin

w:=w+strtoint(copy(currtostr(int),cd-q+1,1));

end ;

if w=0 then

begin

result:=result+'億';

i:=cd-9;

end

else

begin

result:=result+'零';

i:=i+1;

ling:=true;

end;

endelse if cd-i+1>6 then

…… ……

begin

w:=0;

for q:=1 to cd-i+1 do

begin

w:=w+strtoint(copy(currtostr(int),cd-q+1,1));

end ;

if w=0 then

begin

result:=result+'整';

i:=cd+1 ;

endelse

begin

result:=result+'零';

i:=i+1;

ling:=true;

end;

end;

endelse if (copy(currtostr(int),i,1)= '0') and (ling=true) then

begin

i:=i+1;

end;

end;

if xx<0 then result:= '負'+result;

end;

以上程式在windows 2000(windows 98)+delphi 6中除錯通過。

人民幣大寫轉換

人民幣大寫轉換 param numbervalue 人民幣小寫 return rmbcapital function numbervalue var chinesevalue 轉換後的漢字金額 var string1 零壹貳叄肆伍陸柒捌玖 漢字數字 var string2 萬仟佰拾億仟佰拾萬仟佰拾元...

人民幣數字大寫轉換

中文大寫金額數字應用壹 貳 叄 肆 伍 陸 柒 捌 玖 拾 佰 仟 萬 億 元 角 分 零 整 正 等字樣。中文大寫金額數字到 元 為止的,在 元 之後,應寫 整 或 正 字,在 角 之後,可以不寫 整 或 正 字。中文大寫金額數字前應標明 人民幣 字樣,大寫金額數字有 分 的,分 後面不寫 整 或...

人民幣 Rmb 數字轉換大寫 大寫

package com.test public class rmbconvert 段內的量度 char vunit 段間的量度 char digit 小寫對應的大寫 long longmoney long money 100 string strmoney string.valueof longmo...