閒來無事,一時心血來潮,寫了個數字轉人民幣大寫的例子。其中主要的難點是“0”在不同位置,處理的方式有所不同。
這裡考慮的是整數位為最多12位,小數位2位的通用數字。也就是最小值0.00,最大值為999999999999.99。
從左往右看,首先數字在整數部第4位,第8位與第12位為“0”時,不應轉化為大寫“零”。而在小數部的第2位也不應為零。
再者,若是某處數字為“0”,那麼如果其前一位也是“0”的話,則同樣不應轉化為“零”,否則會出現“零零”這樣的結果。
但是這有例外,比如100100.00這樣的數字,第三位是“0”,第二位也是“0”,而按照習慣,是應該轉成“壹拾萬零壹佰元整”的。
所以要考慮將12位整數分為三個部分。若不足12位的話,前面加“0”補足。
當某一部分的某一數字不滿足前一位不為“0”這一條件時,先要從當前部分查看是否之前有過非“0”的數字,若有則不需要加“零”。
如要當前部分還是不滿足條件的話,則要繼續向前查找,在前一部分尋找是否有非“0”的數字。找到的話則加“零”。
小數部分有個地方需要注意:當整數部分為0,小數部分第一位也是“0”時,不要將其轉為“零”。
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace Demo
{
internal class Program
{
private static Dictionary<char, string> _dic = new Dictionary<char, string>();
public static void Main(string[] args)
{
_dic['1'] = "壹";
_dic['2'] = "貳";
_dic['3'] = "三";
_dic['4'] = "肆";
_dic['5'] = "伍";
_dic['6'] = "陸";
_dic['7'] = "柒";
_dic['8'] = "捌";
_dic['9'] = "玖";
_dic['0'] = "零";
while (true)
{
Test();
Thread.Sleep(1000);
}
}
private static void Test()
{
Random r = new Random();
int count = r.Next(0, 13);
int i = 0;
StringBuilder sb = new StringBuilder();
while (i++ < count)
{
sb.Append(r.Next(0, 10));
}
sb.Append(".");
i = 0;
while (i++ < 2)
{
sb.Append(r.Next(0, 10));
}
Console.WriteLine(ConverToRMB(Convert.ToDecimal(sb.ToString(), CultureInfo.InvariantCulture)));
}
private static string ConverToRMB(decimal num)
{
string data = Convert.ToString(num, CultureInfo.InvariantCulture);
bool isNum = ValidateData(data);
if (!isNum)
{
return "無效的數字";
}
string[] parts = data.Split('.');
string intPart = parts[0];
string decPart = parts[1];
string intResult = ParseIntPart(intPart);
bool isZero = String.IsNullOrEmpty(intResult);
string decResult = ParseDecPart(decPart, isZero);
string result = string.Empty;
if (String.IsNullOrEmpty(intResult))
{
if (String.IsNullOrEmpty(decResult))
{
result = "零元整";
}
else
{
result = decResult;
}
}
else
{
if (String.IsNullOrEmpty(decResult))
{
result = intResult + "元整";
}
else
{
result = intResult + "元" + decResult;
}
}
return result;
}
private static bool ValidateData(string data)
{
Regex r = new Regex(@"^\d{0,12}\.\d{2}$");
return r.IsMatch(data);
}
private static string ParseIntPart(string intPart)
{
char[] arr = intPart.ToCharArray();
char[] temp = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
arr.CopyTo(temp, 12 - arr.Length);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 3; i++)
{
StringBuilder s = new StringBuilder();
bool flag = false;
for (int j = 0; j < 4; j++)
{
char c = temp[j + i * 4];
if (c != '0')
{
s.Append(_dic[c]);
switch (j)
{
case 0: s.Append('仟'); break;
case 1: s.Append('佰'); break;
case 2: s.Append('拾'); break;
}
}
else
{
if (j != 3)
{
if (j != 0 && temp[j + i * 4 - 1] != '0')
{
for (int k = j; k < 4; k++)
{
if (temp[k + i * 4] != '0')
{
s.Append("零");
break;
}
}
}
else
{
if (i > 0)
{
for (int k = j; k >= 0; k--)
{
if (temp[k + i * 4] != '0')
{
flag = true;
break;
}
}
if (!flag)
{
bool flag2 = false;
for (int k = i-1; k >= 0; k--)
{
if (!flag2)
{
for (int m = 0; m < 4; m++)
{
if (temp[m + k * 4] != '0')
{
s.Append("零");
flag2 = true;
flag = true;
break;
}
}
}
}
}
}
}
}
}
}
if (s.Length > 0)
{
switch (i)
{
case 0: s.Append('億'); break;
case 1: s.Append('萬'); break;
}
sb.Append(s);
}
}
return sb.ToString();
}
private static string ParseDecPart(string decPart, bool isZero)
{
char[] arr = decPart.ToCharArray();
char c1 = arr[0];
char c2 = arr[1];
if (c1 == '0' && c2 == '0')
{
return string.Empty;
}
StringBuilder sb = new StringBuilder();
if (c1 != '0')
{
sb.Append(_dic[c1]);
sb.Append('角');
}
else
{
if (!isZero)
{
sb.Append("零");
}
}
if (c2 != '0')
{
sb.Append(_dic[c2]);
sb.Append('分');
}
return sb.ToString();
}
}
}
原文同步發布於我的個人博客