public sealed class SAFCFormater
{
/// <summary>
/// 文本格式到HTML
/// </summary>
/// <param name=”src”>源字符</param>
/// <returns>返回安全转换后字符</returns>
public static string TextToHtml_Ex(string src)
{
StringBuilder str = new StringBuilder(HttpUtility.HtmlDecode(src.ToString().Trim()));
str.Replace(“<“, “&lt;”);
str.Replace(“>”, “&gt;”);
str.Replace(“\r\n”, “<br />”);
str.Replace(“\””, “&quot;”);
str.Replace(“\\'”, “&acute;”);
str.Replace(“\0”, “&nbsp”);
return str.ToString();
}
/// <summary>
/// HTML到文本格式
/// </summary>
/// <param name=”src”>源字符</param>
/// <returns>返回安全转换后字符</returns>
public static string HtmlToText_Ex(string src)
{
StringBuilder str = new StringBuilder(HttpUtility.HtmlDecode(src.ToString().Trim()));
str.Replace(“<br />”, “\r\n”);
str.Replace(“&quot;”, “\””);
str.Replace(“&acute;”, “\\'”);
str.Replace(“&nbsp”, “\0”);
return str.ToString();
}

#region 金额大写转换

public static string ToUpper(string currencyDigits)
{
var isZero = “”;
//验证接收的字符串是否金额.wait for add
if (currencyDigits.Substring(0, 1) == “-“)
{
isZero = “负”;
currencyDigits = currencyDigits.Substring(1, currencyDigits.Length – 1);
}

var CN_ZERO = “零”;
var CN_ONE = “壹”;
var CN_TWO = “贰”;
var CN_THREE = “叁”;
var CN_FOUR = “肆”;
var CN_FIVE = “伍”;
var CN_SIX = “陆”;
var CN_SEVEN = “柒”;
var CN_EIGHT = “捌”;
var CN_NINE = “玖”;
var CN_TEN = “拾”;
var CN_HUNDRED = “佰”;
var CN_THOUSAND = “仟”;
var CN_TEN_THOUSAND = “万”;
var CN_HUNDRED_MILLION = “亿”;
var CN_TEN_THOUSAND_BILLION = “兆”;//金额上的兆亿=万亿
var CN_DOLLAR = “元”;
var CN_TEN_CENT = “角”;
var CN_CENT = “分”;
var CN_LI = “厘”;
var CN_INTEGER = “整”;

string integralPart = “”;//整数部分
string decimalPart = “”;//小数部分
var parts = currencyDigits.Split(\’.\’);
if (parts.Length > 1)
{
integralPart = parts[0];
decimalPart = parts[1];
decimalPart = decimalPart.Length > 3 ? decimalPart.Substring(0, 3) : decimalPart;
}
else
{
integralPart = parts[0];
decimalPart = “”;
}
var digits = new string[] { CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE };
var radices = new string[] { “”, CN_TEN, CN_HUNDRED, CN_THOUSAND };//拾佰仟
var bigRadices = new string[] { “”, CN_TEN_THOUSAND, CN_HUNDRED_MILLION, CN_TEN_THOUSAND_BILLION };//万亿兆
var decimals = new string[] { CN_TEN_CENT, CN_CENT, CN_LI };//角分

var outputCharacters = “”;//输出大写字符串
if (Convert.ToInt64(integralPart) > 0)
{
var zeroCount = 0;
for (var i = 0; i < integralPart.Length; i++)
{
var p = integralPart.Length – i – 1;
var d = integralPart.Substring(i, 1);
var quotient = p / 4;
var modulus = p % 4;
if (d == “0”)
{
zeroCount++;
}
else
{
if (zeroCount > 0)
{
outputCharacters += digits[0];
}
zeroCount = 0;
outputCharacters += digits[Convert.ToInt32(d)] + radices[modulus];
}

if (modulus == 0 && zeroCount < 4)
{
outputCharacters += bigRadices[quotient];
}
}

outputCharacters += CN_DOLLAR;
}

if (decimalPart != “”)
{
for (var i = 0; i < decimalPart.Length; i++)
{
var d = decimalPart.Substring(i, 1);
if (d != “0”)
{
outputCharacters += digits[Convert.ToInt32(d)] + decimals[i];
}
}
}

if (outputCharacters == “”)
{
outputCharacters = CN_ZERO + CN_DOLLAR;
}

if (decimalPart == “” || int.Parse(decimalPart.PadRight(3, \’0\’).Substring(1, decimalPart.PadRight(3, \’0\’).Length – 1)) == 0)
{
outputCharacters += CN_INTEGER;
}
return isZero + outputCharacters;

}

#endregion

#region 金额大写转换
private static readonly String[] m_Number = { “零”, “壹”, “贰”, “叁”, “肆”, “伍”, “陆”, “柒”, “捌”, “玖” };
private static readonly String[] m_Unit = { “分”, “角”, “元”, “拾”, “佰”, “仟”, “万”, “拾”, “佰仟亿”, “拾佰仟兆拾佰仟” };
public static String ToUpperOld(String MoneyString)
{
if (IsNumeric(MoneyString) == false) return “”;

decimal num = Convert.ToDecimal(MoneyString);
string str1 = “零壹贰叁肆伍陆柒捌玖”; //0-9所对应的汉字
string str2 = “万仟佰拾亿仟佰拾万仟佰拾元角分”; //数字位所对应的汉字
string str3 = “”; //从原num值中取出的值
string str4 = “”; //数字的字符串形式
string str5 = “”; //人民币大写金额形式
int j; //num的值乘以100的字符串长度
string ch1 = “”; //数字的汉语读法
string ch2 = “”; //数字位的汉字读法
int nzero = 0; //用来计算连续的零值是几个
int temp; //从原num值中取出的值

num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数
str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式
j = str4.Length; //找出最高位
if (j > 15)
{
return “溢出”;
}
str2 = str2.Substring(15 – j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分

//循环取出每一位需要转换的值
for (int i = 0; i < j; i++)
{
str3 = str4.Substring(i, 1); //取出需转换的某一位的值
temp = Convert.ToInt32(str3); //转换为数字
if (i != (j – 3) && i != (j – 7) && i != (j – 11) && i != (j – 15))
{
//当所取位数不为元、万、亿、万亿上的数字时
if (str3 == “0”)
{
ch1 = “”;
ch2 = “”;
nzero = nzero + 1;
}
else
{
if (str3 != “0” && nzero != 0)
{
ch1 = “零” + str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
ch1 = str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
}
}
else
{
//该位是万亿,亿,万,元位等关键位
if (str3 != “0” && nzero != 0)
{
ch1 = “零” + str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
if (str3 != “0” && nzero == 0)
{
ch1 = str1.Substring(temp * 1, 1);
ch2 = str2.Substring(i, 1);
nzero = 0;
}
else
{
if (str3 == “0” && nzero >= 3)
{
ch1 = “”;
ch2 = “”;
nzero = nzero + 1;
}
else
{
if (j >= 11)
{
ch1 = “”;
nzero = nzero + 1;
}
else
{
ch1 = “”;
ch2 = str2.Substring(i, 1);
nzero = nzero + 1;
}
}
}
}
}
if (i == (j – 11) || i == (j – 3))
{
//如果该位是亿位或元位,则必须写上
ch2 = str2.Substring(i, 1);
}
str5 = str5 + ch1 + ch2;
if (i == j – 1 && str3 == “0”)
{
//最后一位(分)为0时,加上“整”
str5 = str5 + \’整\’;
}
}
if (num == 0)
{
str5 = “零元整”;
}
return str5;
}
#endregion

#region 判断是否为数字
public static bool IsNumeric(string i_value)
{
try
{
Regex t_regex = new System.Text.RegularExpressions.Regex(“^(-?[0-9]*[.]*[0-9]{0,12})$”);
if (t_regex.IsMatch(i_value)) return true;
else return false;
}
catch (Exception)
{
return false;
}

}
#endregion

#region 格式化成金额格式输出
/// <summary>
///
/// </summary>
/// <param name=”i_Value”>需要格式化的数值</param>
/// <param name=”i_Show_Flag”>是否显示金额符号</param>
/// <returns></returns>
public static string FormateToMoney(string i_Value, bool i_Show_Flag)
{
double i_temp_value = 0;
if (i_Value == “”) return “”;
if (IsNumeric(i_Value) == false) return “”;
try
{
NumberFormatInfo t_nfi = new CultureInfo(“zh-CN”, false).NumberFormat;

t_nfi.CurrencySymbol = “”;

i_temp_value = Double.Parse(i_Value);

i_temp_value = Math.Round(i_temp_value, 2);

if (i_Show_Flag == true)//显示前面的金额符号
{
return “¥” + i_temp_value.ToString(“C2”, t_nfi);
}
else
{
return i_temp_value.ToString(“C2”, t_nfi);
}

}
catch (Exception ex)
{
if (i_Show_Flag == true)//显示前面的金额符号
{
return “¥0.00”;
}
else
{
return “0.00”;
}
}

}
/// <summary>
/// 数字转化为会计金额(负值为红色)
/// </summary>
/// <param name=”i_Value”></param>
/// <param name=”i_Show_Flag”></param>
/// <returns></returns>
public static string FormateToAccountMoney(string i_Value, bool i_Show_Flag)
{
double i_temp_value = 0;
bool ifnegative = false;
if (i_Value == “”) return “”;
if (IsNumeric(i_Value) == false) return “”;
try
{
NumberFormatInfo t_nfi = new CultureInfo(“zh-CN”, false).NumberFormat;

t_nfi.CurrencySymbol = “”;

i_temp_value = Double.Parse(i_Value);

ifnegative = i_temp_value < 0 ? true : false;

i_temp_value = Math.Abs(Math.Round(i_temp_value, 2));

if (i_Show_Flag == true)//显示前面的金额符号
{
if (ifnegative)
return “<font color=\’red\’>¥” + i_temp_value.ToString(“C2”, t_nfi) + “</font>”;
else
return “¥” + i_temp_value.ToString(“C2”, t_nfi);
}
else
{
if (ifnegative)
return “<font color=\’red\’>” + i_temp_value.ToString(“C2”, t_nfi) + “</font>”;
else
return i_temp_value.ToString(“C2”, t_nfi);
}

}
catch (Exception ex)
{
if (i_Show_Flag == true)//显示前面的金额符号
{
return “¥0.00”;
}
else
{
return “0.00”;
}
}

}
#endregion

#region 格式化成数量格式输出
/// <summary>
///
/// </summary>
/// <param name=”i_Value”>需要格式化的数值</param>
/// <param name=”i_Show_Flag”>是否显示金额符号</param>
/// <returns></returns>
public static string FormateToNum(string i_Value)
{
double i_temp_value = 0;
if (i_Value == “”) return “”;
if (IsNumeric(i_Value) == false) return “”;
try
{
NumberFormatInfo t_nfi = new CultureInfo(“zh-CN”, false).NumberFormat;

t_nfi.CurrencySymbol = “”;

i_temp_value = Double.Parse(i_Value);

i_temp_value = Math.Round(i_temp_value, 3);

return i_temp_value.ToString(“F3”, t_nfi);
}
catch (Exception ex)
{
return “”;
}

}
#endregion

#region 格式化成价格模式输出

public static string FormateToPrice(string i_Value, bool i_Show_Flag)
{
double i_temp_value = 0;
if (i_Value == “”) return “”;
if (IsNumeric(i_Value) == false) return “”;
try
{
NumberFormatInfo t_nfi = new CultureInfo(“zh-CN”, false).NumberFormat;

t_nfi.CurrencySymbol = “”;

i_temp_value = Double.Parse(i_Value);

i_temp_value = Math.Round(i_temp_value, 2);

if (i_Show_Flag == true)//显示前面的金额符号
{
return “¥” + i_temp_value.ToString(“C2”, t_nfi);
}
else
{
return i_temp_value.ToString(“C2”, t_nfi);
}

}
catch (Exception ex)
{
if (i_Show_Flag == true)//显示前面的金额符号
{
return “¥0.00”;
}
else
{
return “0.00”;
}
}

}
#endregion

#region 格式化成百分比模式输出

public static string FormateToPercent(string i_Value)
{
if (i_Value == “”) return “”;
if (IsNumeric(i_Value) == false) return “”;
try
{
var per = (Convert.ToDecimal(i_Value) * 100).ToString();
while (per.EndsWith(“0”))
{
per = per.Substring(0, per.Length – 1);
}
return per + “%”;

}
catch (Exception ex)
{
return “”;
}

}
#endregion

#region 格式化为绝对值进行显示

public static string FormateToAbsNum(string i_Value)
{
string i_temp_value = “”;

if (i_Value == “”)
return “”;
if (IsNumeric(i_Value) == false)
return “”;

try
{
i_temp_value = Math.Abs(Convert.ToDecimal(i_Value)).ToString();
return i_temp_value;
}
catch
{
return “”;
}
}

#endregion
#region 正数显示
public static string PositiveNum(string str)
{
if (str!=””&&int.Parse(str) > 0)
{
return str.ToString();
}
else
{
return “”;
}
}
#endregion

#region 普通字符串与UniCode字符串间转换
/// <summary>
/// 字符串转为UniCode码字符串
/// </summary>
/// <param name=”s”></param>
/// <returns></returns>
public static string StringToUnicode(string s)
{
char[] charbuffers = s.ToCharArray();
byte[] buffer;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < charbuffers.Length; i++)
{
buffer = System.Text.Encoding.Unicode.GetBytes(charbuffers[i].ToString());
sb.Append(String.Format(“\\u{0:X2}{1:X2}”, buffer[1], buffer[0]));
}
return sb.ToString();
}

/// <summary>
/// Unicode字符串转为正常字符串
/// </summary>
/// <param name=”srcText”></param>
/// <returns></returns>
public static string UnicodeToString(string srcText)
{
string dst = “”;
string src = srcText;
int len = srcText.Length / 6;
for (int i = 0; i <= len – 1; i++)
{
string str = “”;
str = src.Substring(0, 6).Substring(2);
src = src.Substring(6);
byte[] bytes = new byte[2];
bytes[1] = byte.Parse(int.Parse(str.Substring(0, 2), NumberStyles.HexNumber).ToString());
bytes[0] = byte.Parse(int.Parse(str.Substring(2, 2), NumberStyles.HexNumber).ToString());
dst += Encoding.Unicode.GetString(bytes);
}
return dst;
}
#endregion

版权声明:本文为zjx-hn原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zjx-hn/p/9018574.html