Last active
August 29, 2015 14:24
-
-
Save edward-hsu-1994/399a0220ac4f3ec1bacc to your computer and use it in GitHub Desktop.
整數數值 與 中文數字 與 英文數字 互相轉換(數值轉中文數字字串、中文數字字串轉數值)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//數字轉換為中文數字字串 | |
public static class NumberExtension { | |
#region decimal的數學指數計算 | |
public static decimal Power(decimal A ,decimal B) { | |
decimal Result = 1; | |
for(int i = 0; i < B; i++) | |
Result *= A; | |
return Result; | |
} | |
#endregion | |
#region 中文單位對應字典 | |
static string[] ChineseNumberDictionary = "零壹貳參肆伍陸柒捌玖".ToCharArray().Select(item => new string(item ,1)).ToArray(); | |
static List<KeyValuePair<decimal ,string>> ChineseUnitDictionary = new List<KeyValuePair<decimal ,string>>() { | |
new KeyValuePair<decimal,string>(Power(10, 0), "" ), | |
new KeyValuePair<decimal,string>(Power(10, 1),"拾"), | |
new KeyValuePair<decimal,string>(Power(10, 2),"佰"), | |
new KeyValuePair<decimal,string>(Power(10, 3),"仟"), | |
new KeyValuePair<decimal,string>(Power(10, 4),"萬"), | |
new KeyValuePair<decimal,string>(Power(10, 8),"億"), | |
new KeyValuePair<decimal,string>(Power(10,12),"兆"), | |
}; | |
#endregion | |
#region 英文單位對應字典 | |
static string[] EnglishNumberDictionary = "zero,one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen,twenty,twentyone,twentytwo,twentythree,twentyfour,twentyfive,twentysix,twentyseven,twentyeight,twentynine,thirty,thirtyone,thirtytwo,thirtythree,thirtyfour,thirtyfive,thirtysix,thirtyseven,thirtyeight,thirtynine,forty,fortyone,fortytwo,fortythree,fortyfour,fortyfive,fortysix,fortyseven,fortyeight,fortynine,fifty,fiftyone,fiftytwo,fiftythree,fiftyfour,fiftyfive,fiftysix,fiftyseven,fiftyeight,fiftynine,sixty,sixtyone,sixtytwo,sixtythree,sixtyfour,sixtyfive,sixtysix,sixtyseven,sixtyeight,sixtynine,seventy,seventyone,seventytwo,seventythree,seventyfour,seventyfive,seventysix,seventyseven,seventyeight,seventynine,eighty,eightyone,eightytwo,eightythree,eightyfour,eightyfive,eightysix,eightyseven,eightyeight,eightynine,ninety,ninetyone,ninetytwo,ninetythree,ninetyfour,ninetyfive,ninetysix,ninetyseven,ninetyeight,ninetynine".Split(','); | |
static List<KeyValuePair<decimal ,string>> EnglishUnitDictionary = new List<KeyValuePair<decimal ,string>>() { | |
new KeyValuePair<decimal,string>(Power(10, 0), "" ), | |
new KeyValuePair<decimal,string>(Power(10, 2), "hundred" ), | |
new KeyValuePair<decimal,string>(Power(10, 3), "thousand"), | |
new KeyValuePair<decimal,string>(Power(10, 6), "million"), | |
new KeyValuePair<decimal,string>(Power(10, 9), "billion"), | |
}; | |
#endregion | |
public static decimal FromChineseNumber(string ChineseNumber) { | |
ChineseNumber = ChineseNumber.Replace(ChineseNumberDictionary[0] ,""); | |
if(ChineseNumber.Length == 0)return 0; | |
KeyValuePair<decimal ,string> UnitValue = (from t in ChineseUnitDictionary where ChineseNumber.IndexOf(t.Value) > -1 orderby t.Key descending select t).FirstOrDefault(); | |
if(UnitValue.Key == 1)return Array.IndexOf(ChineseNumberDictionary ,ChineseNumber); | |
string[] ChineseNumberSegments = ChineseNumber.Split(new string[]{UnitValue.Value},StringSplitOptions.None); | |
decimal AddUnitValue = 1M,Result = 0M; | |
for(int i = ChineseNumberSegments.GetUpperBound(0); i > -1; i--) { | |
Result += FromChineseNumber(ChineseNumberSegments[i]) * AddUnitValue; | |
AddUnitValue *= UnitValue.Key; | |
} | |
return Result; | |
} | |
public static string ToChineseNumber(decimal Value) { | |
KeyValuePair<decimal ,string> UnitValue = (from t in ChineseUnitDictionary where Value - t.Key >= 0 orderby t.Key descending select t).FirstOrDefault(); | |
if(UnitValue.Key <= 1) | |
return ChineseNumberDictionary[(long)Value]; | |
decimal SecondSegment ,FirstSegment = (Value - (SecondSegment = Value % UnitValue.Key)) / UnitValue.Key; | |
if(SecondSegment == 0) { | |
return ToChineseNumber(FirstSegment) + UnitValue.Value; | |
} else { | |
bool NeedZero = (Math.Floor(Math.Log10((double)UnitValue.Key)) - Math.Floor(Math.Log10((double)SecondSegment))) > 1; | |
var Zero = NeedZero ? ToChineseNumber(0) : ""; | |
return ToChineseNumber(FirstSegment) + UnitValue.Value + Zero + ToChineseNumber(SecondSegment); | |
} | |
} | |
public static string ToEnglishNumber(decimal Value) { | |
KeyValuePair<decimal ,string> UnitValue = (from t in EnglishUnitDictionary where Value - t.Key >= 0 orderby t.Key descending select t).FirstOrDefault(); | |
if(UnitValue.Key <= 1) | |
return EnglishNumberDictionary[(long)Value]; | |
decimal SecondSegment ,FirstSegment = (Value - (SecondSegment = Value % UnitValue.Key)) / UnitValue.Key; | |
if(SecondSegment == 0) { | |
return ToEnglishNumber(FirstSegment) + " " + UnitValue.Value; | |
} else { | |
return ToEnglishNumber(FirstSegment) + " " + UnitValue.Value + " " + ToEnglishNumber(SecondSegment); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment