咱也写个小写数字转大写金额 ,纯粹字符串操作实现
纯粹字符串操作实现
using System;
using System.Collections.Generic;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
![](/Images/OutliningIndicators/InBlock.gif)
Console.Write("输入任意数字,回车转换");
Console.Write("\r\n");
Console.Write("直接回车则退出");
Console.Write("\r\n");
![](/Images/OutliningIndicators/InBlock.gif)
for (; ; )
{
string tmpString = Console.ReadLine();
![](/Images/OutliningIndicators/InBlock.gif)
if (tmpString.Trim() == string.Empty)
{
break;
}
try
{
Console.Write(string.Format("{0} \r\n", ConvertNumberToChineseNumber(tmpString)));
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
/// <summary>
/// 数字转大写金额
/// </summary>
/// <param name="targetString">数字</param>
/// <returns>大写金额</returns>
private static string ConvertNumberToChineseNumber(string targetString)
{
string rtnValue = string.Empty;
![](/Images/OutliningIndicators/InBlock.gif)
double tmpValue = 0;
![](/Images/OutliningIndicators/InBlock.gif)
if (double.TryParse(targetString, out tmpValue))
{
rtnValue = tmpValue.ToString("#千兆#百兆#拾兆#兆#仟亿#百亿#拾亿#亿#千万#百万#拾万#万#仟#佰#拾#元.#角#分");
}
else
{
throw new Exception("输入的不是数字或者过超出范围!");
}
![](/Images/OutliningIndicators/InBlock.gif)
//去个头
int firstNumberIndex = rtnValue.IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
![](/Images/OutliningIndicators/InBlock.gif)
if (firstNumberIndex >= 0)
{
rtnValue = rtnValue.Substring(firstNumberIndex);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//去小数点或去个尾
int lastNumberIndex = rtnValue.LastIndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
![](/Images/OutliningIndicators/InBlock.gif)
rtnValue = rtnValue.Substring(0, lastNumberIndex + 2);
![](/Images/OutliningIndicators/InBlock.gif)
//小写大写切换
![](/Images/OutliningIndicators/InBlock.gif)
rtnValue = rtnValue.Replace(".", "");
rtnValue = rtnValue.Replace("0", "零");
rtnValue = rtnValue.Replace("1", "壹");
rtnValue = rtnValue.Replace("2", "贰");
rtnValue = rtnValue.Replace("3", "叁");
rtnValue = rtnValue.Replace("4", "肆");
rtnValue = rtnValue.Replace("5", "伍");
rtnValue = rtnValue.Replace("6", "陆");
rtnValue = rtnValue.Replace("7", "柒");
rtnValue = rtnValue.Replace("8", "捌");
rtnValue = rtnValue.Replace("9", "玖");
![](/Images/OutliningIndicators/InBlock.gif)
return rtnValue;
}
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)