使用C 實現阿拉伯數字到大寫中文的轉換

2021-04-13 04:17:45 字數 3314 閱讀 4046

原始碼下載

using system;

///

/// 本類實現阿拉伯數字到大寫中文的轉換

/// 該類沒有對非法數字進行判別

/// 請呼叫numtochn方法

///

public class numformat

public numformat()

// todo: 在此處新增建構函式邏輯

// 轉換數字

private char tonum(char x)

string strchnnames="零一二三四五六七**";

string strnumnames="0123456789";

return strchnnames[strnumnames.indexof(x)];

// 轉換萬以下整數

private string changeint(string x)

string strarraylevelnames=new string[4] ;

string ret = "";

int i;

for (i=x.length-1;i>=0;i--)

if (x[i] == '0')

ret = tonum(x[i]) + ret;

else

ret = tonum(x[i]) + strarraylevelnames[x.length-1-i] + ret;

while ((i=ret.indexof("零零"))!=-1)

ret=ret.remove(i, 1);

if (ret[ret.length-1]=='零' && ret.length>1)

ret=ret.remove(ret.length-1,1);

if (ret.length>=2 && ret.substring(0,2)=="一十")

ret=ret.remove(0,1);

return ret;

// 轉換整數

private string toint(string x)

int len = x.length;

string ret,temp;

if (len<=4)

ret = changeint(x);

else if (len<=8)

ret = changeint(x.substring(0,len-4)) + "萬";

temp = changeint(x.substring(len-4,4));

if (temp.indexof("千")==-1 && temp!="")

ret += "零" + temp;

else

ret += temp;

else

ret=changeint(x.substring(0,len-8)) + "億";

temp=changeint(x.substring(len-8,4));

if (temp.indexof("千")==-1 && temp!="")

ret += "零" + temp;

else

ret += temp;

ret += "萬";

temp = changeint(x.substring(len-4,4));

if (temp.indexof("千")==-1 && temp!="")

ret += "零" + temp;

else

ret += temp;

int i;

if ((i=ret.indexof("零萬"))!=-1)

ret = ret.remove(i+1,1);

while ((i=ret.indexof("零零"))!=-1)

ret = ret.remove(i,1);

if (ret[ret.length-1]=='零' && ret.length>1)

ret = ret.remove(ret.length-1,1);

return ret;

private string todecimal(string x)

string ret="";

for (int i=0;iret += tonum(x[i]);

return ret;

public string numtochn(string x)

if (x.length==0)

return "";

string ret="";

if (x[0]=='-')

ret="負";

x=x.remove(0,1);

if (x[0].tostring()==".")

x="0"+x;

if (x[x.length-1].tostring()==".")

x=x.remove(x.length-1,1);

if (x.indexof(".")>-1)

ret += toint(x.substring(0,x.indexof(".")))+"點"+todecimal(x.substring(x.indexof(".")+1));

else

ret += toint(x);

return ret;

測試工程

using system;

class mainclass

static void main(string args)

system.console.writeline("hello, the world!");

system.console.writeline("my love!");

classtest ct = new classtest();

system.console.writeline(ct.get_str());

// 過載運算子

myvector v1 = new myvector(5, 12);

myvector v2 = new myvector(4, 3);

myvector v3 = new myvector();

v3 = v1 + v2;

system.console.writeline("測試一下", v3.length);

// 轉換成大寫數字

numformat nf = new numformat();

string x;

while (true)

console.write("x=");

x = console.readline();

if (x == "") break;

console.writeline("=", x, nf.numtochn(x));

使用C 實現阿拉伯數字到大寫中文的轉換

使用c 實現阿拉伯數字到大寫中文的轉換 using system public class numformat 轉換數字 private char tonum char x 轉換萬以下整數 private string changeint string x string ret int i for ...

阿拉伯數字大寫轉換

public class genchinese private final static string str shu ji 大數量級 private final static string str shu ji 2 千內數量級 public genchinese param intnumber 需...

實現阿拉伯數字大寫中文的轉換

本類實現阿拉伯數字到大寫中文的轉換 該類沒有對非法數字進行判別,請事先自己判斷數字是否合法 public class chinesenum public static string getuppermoney double p money 轉換數字 private char chartonum ch...