錢幣的大小寫轉換,123 成一百二十三元整

2021-04-13 05:00:20 字數 3439 閱讀 8046

乙個貨幣數字轉換中文的演算法,注意:本文中的演算法支援小於1023 (也就是9999億兆)貨幣數字轉化。

貨幣中文說明: 在說明**之前,首先讓我們回顧一下貨幣的讀法。

10020002.23  讀為 壹仟零貳萬零貳元貳角叄分

1020         讀為 壹仟零貳拾元整。

100000       讀為 拾萬元整

0.13         讀為 壹角叄分

**:測試工程

static void main(string args)

catch (exception er)

}else

console.writeline("/n請輸入金額");

inputnum = console.readline();

}console.readline();

}測試結果如下:

貨幣轉化類(numcast類)功能介紹

1 常量的規定

///

/// 數字

///

public enum numlevel ;

///

/// 數字的指數

///

private int numlevelexponent = new int ;

///

/// 數字的中文字元

///

private string numleverchinesesign = new string ;

///

/// 大寫字元

///

private string numchinesecharacter = new string ;

///

/// 整(當沒有 角分 時)

///

private const string endofint = "整";

2:數字合法性驗證,採用正規表示式驗證

///

/// 正則表達驗證數字是否合法

///

///

///

public bool isvalidated(t num)

))(/./d)?$");

if (reg.i**atch(num.tostring()))

return false;

}3: 獲取數字 例如 1000的數字為 numlevel.thousand

///

/// 獲取數字的數字 使用log

///

///

///

private numlevel getnumlevel(double num)}}

else

return nlvl;

4:判斷數字之間是否有跳位,也就是中文中間是否要加零,例如1020 就應該加零。

///

/// 是否跳位

///

///

private bool isdumplevel(double num)}}

return false;

5 把長數字分割為兩個較小的數字陣列,例如把9999億兆,分割為9999億和0兆,

因為計算機不支援過長的數字。

///

/// 是否大於兆,如果大於就把字串分為兩部分,

/// 一部分是兆以前的數字

/// 另一部分是兆以後的數字

///

///

///

private bool isbigthantillion(string num)

}else

}return isbig;

}///

/// 把數字字串由『兆』分開兩個

///

///

private double splitnum(string num)

6 是否以「壹拾」開頭,如果是就可以把它變為「拾」

bool isstartoften = false;

while (num >=10)

//num的數字

numlevel currentlevel = getnumlevel(num);

int numexponent = this.numlevelexponent[(int)currentlevel];

num = convert.toint32(math.floor(num / math.pow(10, numexponent)));

if (currentlevel == numlevel.ten && num == 1)

}return isstartoften;

7 合併大於兆連個陣列轉化成的貨幣字串

///

/// 合併分開的陣列中文貨幣字元

///

///

///

private string contactnumchinese(double tillionnums)

else

else

}return chinesecharactor;

8:遞迴計算貨幣數字的中文

///

/// 計算中文字串

///

/// 數字

/// 數字級別 比如1000萬的 數字級別為萬

/// 是否以『壹拾』開頭

/// 中文大寫

public string calculatechinesesign(double num, numlevel? nl ,bool isdump,bool i***ceptten)

else

if (prefixnum < 10 )

else

//加上單位

if (currentlevel == numlevel.yuan )}}

else

//當真正的個位為零時 加上「元」

if (nl == null && postfixnun < 1 && currentlevel > numlevel.yuan && postfixnun > 0)

else

}//是否跳位

// 判斷是否加零, 比如302 就要給三百 後面加零,變為 三百零二。

if (isdumplevel(num))

else if (postfixnun == 0 && currentlevel > numlevel.yuan )

}return result;

}9:外部呼叫的轉換方法。

///

/// 外部呼叫的轉換方法

///

///

///

public string converttochinese(string num)

string chinesecharactor = string.empty;

if (isbigthantillion(num))

else

return chinesecharactor;

}

字元大小寫的轉換

在python中有下面一堆內建函式,用來實現各種型別的大小寫轉化 看例子 a qiwsir,python a.upper 將小寫字母完全變成大寫字母 qiwsir,python a 原資料物件並沒有改變 qiwsir,python b a.upper b qiwsir,python c b.lowe...

C string的大小寫轉換

將乙個string轉換成大寫或者小寫,是專案中經常需要做的事情,但string類裡並沒有提供這個方法。自己寫個函式來實現,說起來挺簡單,但做起來總讓人覺得不方便。打個比方 早上起來想吃個漢堡,冰箱裡有生牛肉,有麵粉,也有微波爐,是可以自己做的,但是實在是太費事,沒幾個人願意做。但是,打個 給肯德基宅...

共享記憶體位元組的大小寫轉換

include include include include include include define buffer size 2048 void strupr char str p int main else printf create share memory n if pid fork ...