Skip to content

Instantly share code, notes, and snippets.

@oldrev
Last active December 23, 2015 12:09
Show Gist options
  • Save oldrev/6633565 to your computer and use it in GitHub Desktop.
Save oldrev/6633565 to your computer and use it in GitHub Desktop.
中文互联网上迄今为止实现最正确、代码最漂亮且效率最高的大写人民币金额转换代码
/** 中文互联网上写的最漂亮效率最高的大写人民币金额转换代码
* 作者:李维 <[email protected]>
* 版权所有 (c) 2013 李维。保留所有权利。
* 本代码基于 BSD License 授权。
* */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RMBUpper {
public class RmbUpperConverter {
private static readonly Char[] RmbDigits = {
'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
private static readonly Dictionary<int, string> DecDigits =
new Dictionary<int, string>() { { 0, "" }, { 1, "拾" }, { 2, "佰" }, { 3, "仟" }, { 5, "万" } };
public static string ToUpper(double price) {
var sb = new StringBuilder();
int nDigits = (int)Math.Floor(Math.Log10(Math.Abs(price))) + 1;
var qianPart = (long)(price % 10000);
var wanPart = ((long)(price % 100000000) - qianPart) / 10000;
var yiPart = ((long)(price % 100000000000) - qianPart - wanPart) / 100000000;
var integerPart = yiPart * 100000000 + wanPart * 10000 + qianPart;
var decPart = (long)(price * 100) % 100;
//处理亿的部分
if (price > 99999999.99) {
ParseInteger(sb, yiPart);
sb.Append("亿");
}
//处理万的部分
if (price > 9999.99) {
if (price > 99999999.99 && wanPart > 0 && wanPart <= 999) {
sb.Append("零");
}
ParseInteger(sb, wanPart);
sb.Append("万");
}
//处理千及以后的部分
if (price > 9999.99 && qianPart > 0 && qianPart <= 999) {
sb.Append("零");
}
ParseInteger(sb, qianPart);
sb.Append("元");
ParseDecimal(sb, decPart);
return sb.ToString();
}
private static void ParseDecimal(StringBuilder sb, long decPart) {
if (decPart <= 0) {
sb.Append("整");
}
else {
if (decPart > 0) {
var jiao = decPart / 10;
var fen = decPart % 10;
if (jiao > 0) {
sb.Append(RmbDigits[jiao]);
sb.Append("角");
}
if (jiao == 0 && fen > 0) {
sb.Append("零");
}
if (fen > 0) {
sb.Append(RmbDigits[fen]);
sb.Append("分");
}
else {
sb.Append("整");
}
}
}
}
private static void ParseInteger(StringBuilder sb, long value) {
var u = Math.Abs(value);
int nDigits = (int)Math.Floor(Math.Log10(u)) + 1;
long lastDigit = -1;
for (var i = 0; i < nDigits; i++) {
var factor = (long)Math.Pow(10, nDigits - 1 - i);
var digit = u / factor;
//几种情况
//遇到零先跳过,下次循环再处理
var skipSpec = false;
if (digit == 0 && i < nDigits - 1) {
skipSpec = true;
} //最后一位是0,则不能转换为“零”
else if (digit == 0 && i >= nDigits - 1) {
skipSpec = false;
}//连续两位都是 0,则只出一个零
else if (lastDigit == 0 && digit == 0 && i < nDigits - 1) {
sb.Append(RmbDigits[digit]);
skipSpec = true;
}
else {
sb.Append(RmbDigits[digit]);
skipSpec = false;
}
if (!skipSpec) {
var f1 = nDigits - i - 1;
if (DecDigits.ContainsKey(f1)) {
sb.Append(DecDigits[f1]);
}
}
u -= u / factor * factor;
lastDigit = digit;
}
}
}
}
@oldrev
Copy link
Author

oldrev commented Sep 20, 2013

@oldrev
Copy link
Author

oldrev commented Sep 21, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment