咱也写个小写数字转大写金额 ,纯粹字符串操作实现

纯粹字符串操作实现

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  ConsoleApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            Console.Write(
"输入任意数字,回车转换");
InBlock.gif            Console.Write(
"\r\n");
InBlock.gif            Console.Write(
"直接回车则退出");
InBlock.gif            Console.Write(
"\r\n");
InBlock.gif
InBlock.gif            
for (; ; )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string tmpString = Console.ReadLine();
InBlock.gif
InBlock.gif                
if (tmpString.Trim() == string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.Write(
string.Format("{0} \r\n", ConvertNumberToChineseNumber(tmpString)));
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.Write(ex.Message);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 数字转大写金额
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="targetString">数字</param>
ExpandedSubBlockEnd.gif        
/// <returns>大写金额</returns>

InBlock.gif        private static string ConvertNumberToChineseNumber(string targetString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string rtnValue = string.Empty;
InBlock.gif
InBlock.gif            
double tmpValue = 0;
InBlock.gif
InBlock.gif            
if (double.TryParse(targetString, out tmpValue))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rtnValue 
= tmpValue.ToString("#千兆#百兆#拾兆#兆#仟亿#百亿#拾亿#亿#千万#百万#拾万#万#仟#佰#拾#元.#角#分");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception("输入的不是数字或者过超出范围!");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//去个头
ExpandedSubBlockStart.gifContractedSubBlock.gif
            int firstNumberIndex = rtnValue.IndexOfAny(new char[] dot.gif'0''1''2''3''4''5''6''7''8''9' });
InBlock.gif
InBlock.gif            
if (firstNumberIndex >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rtnValue 
= rtnValue.Substring(firstNumberIndex);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif            
//去小数点或去个尾
ExpandedSubBlockStart.gifContractedSubBlock.gif
            int lastNumberIndex = rtnValue.LastIndexOfAny(new char[] dot.gif'0''1''2''3''4''5''6''7''8''9' });
InBlock.gif
InBlock.gif            rtnValue 
= rtnValue.Substring(0, lastNumberIndex + 2);
InBlock.gif
InBlock.gif            
//小写大写切换
InBlock.gif

InBlock.gif            rtnValue 
= rtnValue.Replace(".""");
InBlock.gif            rtnValue 
= rtnValue.Replace("0""");
InBlock.gif            rtnValue 
= rtnValue.Replace("1""");
InBlock.gif            rtnValue 
= rtnValue.Replace("2""");
InBlock.gif            rtnValue 
= rtnValue.Replace("3""");
InBlock.gif            rtnValue 
= rtnValue.Replace("4""");
InBlock.gif            rtnValue 
= rtnValue.Replace("5""");
InBlock.gif            rtnValue 
= rtnValue.Replace("6""");
InBlock.gif            rtnValue 
= rtnValue.Replace("7""");
InBlock.gif            rtnValue 
= rtnValue.Replace("8""");
InBlock.gif            rtnValue 
= rtnValue.Replace("9""");
InBlock.gif
InBlock.gif            
return rtnValue;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/sasbya/archive/2007/11/07/951987.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值