程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 會計中阿拉伯數字變數字繁體大寫,阿拉伯大寫

會計中阿拉伯數字變數字繁體大寫,阿拉伯大寫

編輯:C#入門知識

會計中阿拉伯數字變數字繁體大寫,阿拉伯大寫


//注釋:本文非原著是通過網上的代碼修改大約10%而成的,忘了出處了,所以暫時不標明出處

//轉化的工具類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IntegrationSystemChemicalIndustry.Product
{
/// <summary>
  /// 阿拉伯數字轉中文數字,中文數字轉阿拉伯數字。
  /// by hcling97.Net 2007.03
  /// </summary>
public class NumberConventer
{
private string[] ArabinNum = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
private string[] ChnNum = { "零", "壹", "貳", "三", "肆", "伍", "陸", "柒", "捌", "玖", "拾", "佰", "仟", "萬", "億" };

private string[] Union = { "", "拾", "佰", "仟" };
public NumberConventer()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public string ArabToChn(Decimal ArabNum, out string msg)
{
string neg = string.Empty;
neg = (ArabNum < 0) ? "負" : "";
string result = string.Empty;
string[] part =
(ArabNum.ToString().Replace("-", string.Empty)).Split('.');
Int64 temp = Convert.ToInt64(part[0]);
Int64 epart = temp;
string dotpart =
(part.Length > 1) ? part[1] : string.Empty;
if (part.Length > 1)
{
dotpart = this.GetDotPart(dotpart);
}
string tmp = string.Empty;
string lasttemp = string.Empty;
for (int i = 0; i <= ((epart.ToString().Length - 1) / 4); i++)
{
int thousand = 0;
thousand = Convert.ToInt32(temp %
10000);
temp = temp / 10000;
lasttemp = tmp;
tmp = this.GetThousandPart(thousand);

if (i == 0)
{
result = tmp;
lasttemp = tmp;
}
if (i == 1)//返回的是萬
{
if (result == "零")
{
result = string.Empty;
}
result = tmp + "萬" +
((lasttemp.IndexOf("千") == -1 && lasttemp != "零") ? "零" : "") + result;
}
if (i == 2)//億
{
if (result.IndexOf("零萬") != -1)
{

result = result.Replace("零萬", string
.Empty);
}
result = tmp + "億" + ((lasttemp.IndexOf("千") == -1 && lasttemp != "零") ? "零" : "") + result;
}
if (i == 3)//萬億
{
if (result.IndexOf("零億") != -1)
{
result = result.Replace("零億", "億");
}
result = tmp + "萬" + ((lasttemp.IndexOf("千") == -1 && lasttemp != "零") ? "零" : "") + result;
}
if (i == 4)//億億
{
if (result.IndexOf("零萬") != -1)
{

result = result.Replace("零萬", string
.Empty);
}
result = tmp + "億" + ((lasttemp.IndexOf("千") == -1 && lasttemp != "零") ? "零" : "") + result;
}
}
result = neg + result + dotpart;
msg = "成功轉換!";
return result;
}
/// <summary>
/// 處理小數部分
/// </summary>
/// <param name="dotPart"></param>
/// <returns></returns>
private string GetDotPart(string dotPart)
{
string result = "點";
for (int i = 0; i < dotPart.Length; i++)
{
result += ChnNum[Convert.ToInt32(dotPart[i].ToString())];
}
for (int j = 0; j < result.Length; j++)
//去除無效零或點
{

if (result[result.Length - j - 1].ToString() != "點" && result[result.Length - j - 1].ToString() != "零")
{
break;
}
else
{
result =result.Substring(0, (result.Length - j - 1));
}
}
return result;
}
/// <summary>
    /// 萬位以下的分析
    /// </summary>
    /// <returns></returns>
private string GetThousandPart(int number)
{
if (number == 0)
{
return "零";
}
string result = string.Empty;
bool lowZero = false;
//記錄低位有沒有找到非零值,沒找到置true
bool befZero = false;
//記錄前一位是不是非零值,是0則置true
int temp = number;
int index = number.ToString().Length;
for (int i = 0; i < index; i++)
{
int n = temp % 10;
temp = temp / 10;
if (i == 0) //起始位
{
if (n == 0)
{
lowZero = true; //低位有0
befZero = true; //前位為0
}
else
{
result = ChnNum[n];
}
}
else
{
if (n != 0)
{
result = ChnNum[n] + Union[i] + result;
lowZero = false;
befZero = false;
}
else
{
if (!lowZero)
{
if (!befZero)
//低位有數,且前位不為0,本位為0填零
//eg.5906
{
result = ChnNum[n] + result;
befZero = true;
}
else
//低位有數,且前位為0省略零eg. 5008
{
}
}
else //低位為0
{
if (!befZero)//理論上不存在eg 5080
{
result = ChnNum[n] + result;
befZero = true;
}
else //eg. 5000
{
}
}
}
}
}
return result;
}
public Decimal ChnToArab(string ChnNum)
{
Decimal result = 0;
string temp = ChnNum;
bool neg = false;
if (ChnNum.IndexOf("負") != -1)
{
neg = true;
temp = temp.Replace("負", string.Empty);
}
string pre = string.Empty;
string abo = string.Empty;
temp = temp.Replace("點", ".");
string[] part = temp.Split('.');
pre = part[0];
Decimal dotPart = 0;
if (part.Length > 1)
{
abo = part[1];
dotPart = this.GetArabDotPart(abo);
}

int yCount = 0;
//"億"的個數,有可能出現億億。
//int yPos = 0;

int index = 0;
while (index < pre.Length)
{
if (pre.IndexOf("億", index) != -1)
{
yCount++;
//yPos = pre.IndexOf("億",index);
index = pre.IndexOf("億", index) + 1;
}
else
{
break;
}
}
if (yCount == 2)//億億
{
pre = pre.Replace("億", ",");
string[] sp = pre.Split(',');
result =
(neg ? -1 : 1) * ((this.HandlePart(sp[0]) * 10000000000000000) + (this.HandlePart(sp[1]) * 100000000) + this.HandlePart(sp[2])) + dotPart;
}
else
{
if (yCount == 1)
{
pre = pre.Replace("億", ",");
string[] sp = pre.Split(',');
result =(neg ? -1 : 1) * ((this.HandlePart(sp[0]) * 100000000) + this.HandlePart(sp[1])) + dotPart;
}
else
{
if (yCount == 0)
{
result =(neg ? -1 : 1) * this.HandlePart(pre) + dotPart;
}
}
}
return result;

}
/// <summary>
    /// 處理億以下內容。
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
private Decimal HandlePart(string num)
{
Decimal result = 0;
string temp = num;
temp = temp.Replace("萬", ",");
string[] part = temp.Split(',');
for (int i = 0; i < part.Length; i++)
{
result +=Convert.ToDecimal(this.GetArabThousandPart(part[part.Length - i - 1])) * Convert.ToDecimal((System.Math.Pow(10000, Convert.ToDouble(i))));
}
return result;
}

/// <summary>
    /// 取得阿拉伯數字小數部分。
    /// </summary>
    /// <returns></returns>
private Decimal GetArabDotPart(string dotpart)
{
Decimal result = 0.00M;
string spe = "0.";
for (int i = 0; i < dotpart.Length; i++)
{
spe +=this.switchNum(dotpart[i].ToString()).ToString();
}
result = Convert.ToDecimal(spe);
return result;
}

public int GetArabThousandPart(string number)
{

string ChnNumString = number;
if (ChnNumString == "零")
{
return 0;
}
if (ChnNumString != string.Empty)
{
if (ChnNumString[0].ToString() == "十")
{
ChnNumString = "一" + ChnNumString;
}
}

ChnNumString = ChnNumString.Replace("零", string.Empty);
//去除所有的零
int result = 0;
int index = ChnNumString.IndexOf("千");
if (index != -1)
{
result +=this.switchNum(ChnNumString.Substring(0, index)) * 1000;
ChnNumString =ChnNumString.Remove(0, index + 1);
}
index = ChnNumString.IndexOf("百");
if (index != -1)
{
result +=this.switchNum(ChnNumString.Substring(0 , index)) * 100;
ChnNumString = ChnNumString.Remove(0, index + 1);
}
index = ChnNumString.IndexOf("十");
if (index != -1)
{
result += this.switchNum(ChnNumString.Substring(0 , index)) * 10;
ChnNumString = ChnNumString.Remove(0, index + 1);
}
if (ChnNumString != string.Empty)
{
result += this.switchNum(ChnNumString);
}
return result;
}
/// <summary>
    /// 取得漢字對應的阿拉伯數字
    /// </summary>
    /// <param name="n"></param>
    /// <returns></returns>
private int switchNum(string n)
{
switch (n)
{
case "零":
{
return 0;
}
case "一":
{
return 1;
}
case "二":
{
return 2;
}
case "三":
{
return 3;
}
case "四":
{
return 4;
}
case "五":
{
return 5;
}
case "六":
{
return 6;
}
case "七":
{
return 7;
}
case "八":
{
return 8;
}
case "九":
{
return 9;
}
default:
{
return -1;
}
}
}
}
}

 

///頁面調用的方法

NumberConventer NC = new NumberConventer();//新建工作類對象
string TP = string.Empty;//TP轉換結果
this.txtTotalPriceUP.Text = NC.ArabToChn(Convert.ToDecimal(_TotalPrice), out TP);//這個方法返回結果,類型string,能處理的數字大小為一億個億

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved