# 关于一个算法的。。。。。

0

0
```我也尝试写了下，核心代码如下。

#region  常量定义

/// <summary>
/// 亿，万万
/// </summary>
private const int HUNDRED_MILLION = 100000000;

/// <summary>
/// 万，十千
/// </summary>
private const int TEN_THOUSAND = 10000;

private const int THOUSAND = 1000;

private const int HUNDRED = 100;

private const int TEN = 10;

/// <summary>
/// 万亿(不包含)
/// </summary>
private const double MAX_MONEY = 1000000000000;

/// <summary>
/// 金额不能为负，最小值为0.
/// </summary>
private const double MIN_MONEY = 0;

private const string AMOUT_STRING = "零壹贰叁肆伍陆柒捌玖";

#endregion

#region 工具库

private string GetMoneyAmout(double money)
{
var rightMoney = GetMoneyFormat(money);
//整数部分
var longPart = (long) rightMoney;
//小数部分
var decimalPart = rightMoney - longPart;

var longPartAmout = this.ConvertLongPartToAmout(longPart);
var decimalPartAmout = this.ConvertDecimalPartToAmout(decimalPart);

return longPartAmout + (string.IsNullOrEmpty(decimalPartAmout) ? "整" : decimalPartAmout);
}

private double GetMoneyFormat(double money)
{
var d = double.Parse(money.ToString("F2"));
if (d > MAX_MONEY || d < MIN_MONEY)
{
throw new ArgumentOutOfRangeException("Money must greater than or equal 0 and less than 1000,000,000,000");
}
return d;
}

private string ConvertLongPartToAmout(long longPart)
{
var amoutBuilder = new StringBuilder();
while (longPart > 0)
{
if (longPart > HUNDRED_MILLION)
{
amoutBuilder.AppendFormat("{0}亿", this.NumToChinese((int)longPart / HUNDRED_MILLION));
longPart %= HUNDRED_MILLION;
}
else if (longPart > TEN_THOUSAND)
{
amoutBuilder.AppendFormat("{0}万", this.NumToChinese((int)longPart / TEN_THOUSAND));
longPart %= TEN_THOUSAND;
}
else
{
amoutBuilder.AppendFormat("{0}", this.NumToChinese((int)longPart));
longPart = 0;
}
}
amoutBuilder.Append("元");
return amoutBuilder.ToString();
}
/// <summary>
/// 将10000以内的金额转换为中文大写
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
private string NumToChinese(int num)
{
var builder = new StringBuilder();
bool useZero = false;
while (num > 0)
{
if (num >= THOUSAND)
{
builder.AppendFormat("{0}仟", AMOUT_STRING[num / THOUSAND]);
num %= THOUSAND;
useZero = true;
}
else if (num >= HUNDRED)
{
builder.AppendFormat("{0}佰", AMOUT_STRING[num / HUNDRED]);
num %= HUNDRED;
useZero = false;
}
else if (num >= TEN)
{
if (useZero)
{
builder.Append("零");
}
builder.AppendFormat("{0}拾", AMOUT_STRING[num / TEN]);
num %= TEN;
}
else
{
builder.Append(AMOUT_STRING[num]);
num = 0;
}
}
return builder.ToString();
}

private string ConvertDecimalPartToAmout(double decimalPart)
{
return "";
}

#endregion```

0

0

rubekid | 园豆：206 (菜鸟二级) | 2015-04-18 12:57

0

Albert Fei | 园豆：2102 (老鸟四级) | 2015-04-18 13:20

@小林丶2013: 金额转大写，网上也很多，也可以找一些参考参考.

@Albert Fei: 恩 好的，现在正在自己垒代码

0

```    static string ConvertToChinese(double x)
{
string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|\$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|\$))))", "\${b}\${z}");

return Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString());
}```

果然是大神呀，谢谢了

@小林丶2013:  不是我写的，我收集的 :-) 。

@天方: 恩，谢谢

您需要登录以后才能回答，未注册用户请先注册