数字转换成大写金额(C#实现)

None.gif //  例如:(new Money(200)).ToString() == "贰佰元"
None.gif
namespace  Skyiv.Util
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
using System.Text;
InBlock.gif  
class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
for (;;)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        System.Console.Write(
"金额: ");
InBlock.gif        
string  s = System.Console.ReadLine();
InBlock.gif        
decimal m;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try   dot.gif{ m = decimal.Parse(s); }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
catch dot.gifbreak; }
InBlock.gif        System.Console.WriteLine(
"大写: " + new Money(m));
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif  
// 该类重载的 ToString() 方法返回的是大写金额字符串
InBlock.gif
  class Money
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
public string Yuan  = "";                        // “元”,可以改为“圆”、“卢布”之类
InBlock.gif
    public string Jiao  = "";                        // “角”,可以改为“拾”
InBlock.gif
    public string Fen   = "";                        // “分”,可以改为“美分”之类
InBlock.gif
    static string Digit = "零壹贰叁肆伍陆柒捌玖";      // 大写数字
InBlock.gif
    bool   isAllZero    = true;                        // 片段内是否全零
InBlock.gif
    bool   isPreZero    = true;                        // 低一位数字是否是零
InBlock.gif
    bool   Overflow     = false;                       // 溢出标志
InBlock.gif
    long   money100;                                   // 金额*100,即以“分”为单位的金额
InBlock.gif
    long   value;                                      // money100的绝对值
InBlock.gif
    StringBuilder sb    = new StringBuilder();         // 大写金额字符串,逆序
InBlock.gif    
// 只读属性: "零元"
InBlock.gif
    public string ZeroString
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
get dot.gifreturn Digit[0+ Yuan; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// 构造函数
InBlock.gif
    public Money(decimal money)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
try   dot.gif{ money100 = (long)(money * 100m); }
ExpandedSubBlockStart.gifContractedSubBlock.gif      
catch dot.gif{ Overflow = true; }
InBlock.gif      
if (money100 == long.MinValue) Overflow = true;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// 重载 ToString() 方法,返回大写金额字符串
InBlock.gif
    public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
if (Overflow) return "金额超出范围";
InBlock.gif      
if (money100 == 0return ZeroString;
ExpandedSubBlockStart.gifContractedSubBlock.gif      
string [] Unit = dot.gif{ Yuan, """亿""""亿亿" };
InBlock.gif      value 
= System.Math.Abs(money100);
InBlock.gif      ParseSection(
true);
InBlock.gif      
for (int i = 0; i < Unit.Length && value > 0; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
if (isPreZero && !isAllZero) sb.Append(Digit[0]);
InBlock.gif        
if (i == 4 && sb.ToString().EndsWith(Unit[2]))
InBlock.gif          sb.Remove(sb.Length 
- Unit[2].Length, Unit[2].Length);
InBlock.gif        sb.Append(Unit[i]);
InBlock.gif        ParseSection(
false);
InBlock.gif        
if ((i % 2== 1 && isAllZero)
InBlock.gif          sb.Remove(sb.Length 
- Unit[i].Length, Unit[i].Length);
ExpandedSubBlockEnd.gif      }

InBlock.gif      
if (money100 < 0) sb.Append("");
InBlock.gif      
return Reverse();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// 解析“片段”: “角分(2位)”或“万以内的一段(4位)”
InBlock.gif
    void ParseSection(bool isJiaoFen)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
string [] Unit = isJiaoFen ?
ExpandedSubBlockStart.gifContractedSubBlock.gif        
new string [] dot.gif{ Fen, Jiao } :
ExpandedSubBlockStart.gifContractedSubBlock.gif        
new string [] dot.gif"""""""" };
InBlock.gif      isAllZero 
= true;
InBlock.gif      
for (int i = 0; i < Unit.Length && value > 0; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
int d = (int)(value % 10);
InBlock.gif        
if (d != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
if (isPreZero && !isAllZero) sb.Append(Digit[0]);
InBlock.gif          sb.AppendFormat(
"{0}{1}", Unit[i], Digit[d]);
InBlock.gif          isAllZero 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        isPreZero 
= (d == 0);
InBlock.gif        value 
/= 10;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
// 反转字符串
InBlock.gif
    string Reverse()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      StringBuilder sbReversed 
= new StringBuilder();
InBlock.gif      
for (int i = sb.Length - 1; i >= 0; i--)
InBlock.gif        sbReversed.Append(sb[i]);
InBlock.gif      
return sbReversed.ToString();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值