public static decimal GetNumber(string chineseCurrency) { var regex = new Regex(@"(.+)(?:圆|元)(?:整|(.)角整|(.)角(.)分|零(.)分)"); var match = regex.Match(chineseCurrency); if (!match.Success) { throw new ArgumentException("不是合法的中文货币描述。"); } var yuan = match.Groups[1].Value.For(x => ChineseNumber.GetNumber(x)); var jiao = (match.Groups[2].Value.For(x => x.IsWhiteSpace() ? null : x) ?? match.Groups[3].Value.For(x => x.IsWhiteSpace() ? null : x)) ?.For(x => ChineseNumber.GetNumber(x)) ?? 0m; var fen = (match.Groups[4].Value.For(x => x.IsWhiteSpace() ? null : x) ?? match.Groups[5].Value.For(x => x.IsWhiteSpace() ? null : x)) ?.For(x => ChineseNumber.GetNumber(x)) ?? 0m; return(yuan + (jiao * 0.1m) + (fen * 0.01m)); }
public static string GetString(decimal money, ChineseNumberOptions options) { var fractional100 = (int)(money % 1 * 100 % 100); string[] numberValues; string[] levels; if (options.Upper) { numberValues = ChineseNumber.UpperNumberValues; levels = UpperLevels; } else { numberValues = ChineseNumber.LowerNumberValues; levels = LowerLevels; } var yuan = ChineseNumber.GetString(money, options); string ret; if (fractional100 == 0) { ret = $"{yuan}{levels[0]}整"; } else if (fractional100 % 10 == 0) { ret = $"{yuan}{levels[0]}{numberValues[fractional100 / 10]}{levels[1]}整"; } else { var jiao = fractional100 / 10; ret = $"{yuan}{levels[0]}{(jiao > 0 ? $"{numberValues[jiao]}{levels[1]}" : numberValues[0])}{numberValues[fractional100 % 10]}{levels[2]}"; } return(ret); }