You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

710 lines
15 KiB

3 years ago
namespace System
{
/// <summary>
/// ��ʽ��������ת�� ��Ԫ
/// </summary>
public partial class FormatCom
{
private const string Dateformat = "yyyy-MM-dd";
#region ToDecimal
/// <summary>
/// ת��Ϊ ʮ���Ʊ�ʾ
/// </summary>
/// <param name="inStr">�ַ���</param>
/// <returns></returns>
public static Decimal ToDecimal(string inStr)
{
Decimal Result = 0;
if (inStr != null)
{
if (Decimal.TryParse(inStr, out Result) == false)
return 0;
}
return Result;
}
/// <summary>
/// ת��Ϊ ʮ���Ʊ�ʾ
/// </summary>
/// <param name="obj">Ҫת���Ķ���</param>
/// <returns></returns>
public static Decimal ToDecimal(object obj)
{
Decimal Result = 0;
if (null != obj)
{
if (Decimal.TryParse(obj.ToString(), out Result) == false)
return 0;
}
return Result;
}
#endregion ToDecimal
#region ToInt����
/// <summary>
/// �ַ���ת��Ϊ �������ֱ�ʾ
/// </summary>
/// <param name="inStr">��Ҫת�����ַ���</param>
/// <returns></returns>
public static int ToInt(string inStr)
{
int Result = 0;
if (inStr != null)
{
int pos = inStr.IndexOf('.');
if (pos > 0)
{
inStr = inStr.Substring(0, pos);
}
if (int.TryParse(inStr, out Result) == false)
return 0;
}
return Result;
}
/// <summary>
/// Decimal ת��Ϊ �������ֱ�ʾ
/// </summary>
/// <param name="inDec">��Ҫת����ʮ��������</param>
/// <returns></returns>
public static int ToInt(Decimal inDec)
{
return ToInt(inDec.ToString());
}
/// <summary>
/// Decimal ת��Ϊ �������ֱ�ʾ
/// </summary>
/// <param name="inDec">��Ҫת����ʮ��������</param>
/// <returns></returns>
public static int ToInt(double dounumber)
{
return ToInt(dounumber.ToString());
}
/// <summary>
/// object ת��Ϊ �������ֱ�ʾ
/// </summary>
/// <param name="obj">��Ҫת���Ķ���</param>
/// <returns></returns>
public static int ToInt(object obj)
{
if (obj == null) return 0;
return ToInt(obj.ToString());
}
#endregion ToInt����
#region ToInt32����
/// <summary>
/// �ַ���ת��Ϊ �������ֱ�ʾ
/// </summary>
/// <param name="inStr">��Ҫת�����ַ���</param>
/// <returns></returns>
public static int ToInt32(string inStr)
{
int Result = 0;
if (inStr != null)
{
int pos = inStr.IndexOf('.');
if (pos > 0)
{
inStr = inStr.Substring(0, pos);
}
if (int.TryParse(inStr, out Result) == false)
return 0;
}
return Result;
}
/// <summary>
/// Decimal ת��Ϊ �������ֱ�ʾ
/// </summary>
/// <param name="inDec">��Ҫת����ʮ��������</param>
/// <returns></returns>
public static int ToInt32(Decimal inDec)
{
return ToInt(inDec.ToString());
}
/// <summary>
/// Decimal ת��Ϊ �������ֱ�ʾ
/// </summary>
/// <param name="inDec">��Ҫת����ʮ��������</param>
/// <returns></returns>
public static int ToInt32(double dounumber)
{
return ToInt(dounumber.ToString());
}
/// <summary>
/// object ת��Ϊ �������ֱ�ʾ
/// </summary>
/// <param name="obj">��Ҫת���Ķ���</param>
/// <returns></returns>
public static int ToInt32(object obj)
{
if (obj == null) return 0;
return ToInt(obj.ToString());
}
#endregion ToInt32����
#region ToLong����
/// <summary>
/// �ַ���ת��Ϊ long�������ֱ�ʾ
/// </summary>
/// <param name="inStr">��Ҫת�����ַ���</param>
/// <returns></returns>
public static long ToLong(string inStr)
{
long Result = 0;
if (inStr != null)
{
if (long.TryParse(inStr, out Result) == false)
return 0;
}
return Result;
}
/// <summary>
/// Decimal ת��Ϊ long�������ֱ�ʾ
/// </summary>
/// <param name="inDec">��Ҫת����ʮ��������</param>
/// <returns></returns>
public static long ToLong(Decimal inDec)
{
long Result = 0;
string inStr = inDec.ToString();
int pos = inStr.IndexOf('.');
inStr = inStr.Substring(0, pos);
if (inStr != null)
{
if (long.TryParse(inStr, out Result) == false)
return 0;
}
return Result;
}
/// <summary>
/// object ת��Ϊ long�������ֱ�ʾ
/// </summary>
/// <param name="obj">��Ҫת���Ķ���</param>
/// <returns></returns>
public static long ToLong(object obj)
{
long Result = 0;
if (null != obj)
{
if (long.TryParse(obj.ToString(), out Result) == false)
return 0;
}
return Result;
}
#endregion ToLong����
#region toShort����
/// <summary>
/// �ַ���ת��Ϊ ��������ʾ
/// </summary>
/// <param name="inStr"></param>
/// <returns></returns>
public static short ToShort(string inStr)
{
short Result = 0;
if (inStr != null)
{
if (short.TryParse(inStr, out Result) == false)
return 0;
}
return Result;
}
/// <summary>
/// Decimal ת��Ϊ ��������ʾ
/// </summary>
/// <param name="inStr"></param>
/// <returns></returns>
public static short ToShort(Decimal inDec)
{
short Result = 0;
string inStr = inDec.ToString();
int pos = inStr.IndexOf('.');
inStr = inStr.Substring(0, pos);
if (inStr != null)
{
if (short.TryParse(inStr, out Result) == false)
return 0;
}
return Result;
}
/// <summary>
/// object ת��Ϊ ��������ʾ
/// </summary>
/// <param name="inStr"></param>
/// <returns></returns>
public static short ToShort(object obj)
{
short Result = 0;
if (!obj.Equals(null))
{
if (short.TryParse(obj.ToString(), out Result) == false)
return 0;
}
return Result;
}
#endregion toShort����
#region ��������toString()
/// <summary>
/// �����ַ���
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string String(string str)
{
if (str != null)
{
str = str.Replace("&amp;", "&");
str = str.Replace("&nbsp;", " ");
str = str.Replace("<br>", "\n");
str = str.Replace("&gt;", ">");
str = str.Replace("&lt;", "<");
str = str.Replace("&quot;", "\"");
str = str.Replace("''", "'");
str = str.Replace("'", "��");
}
else
str = "";
return str;
}
public static string ToString(string str)
{
return String(str);
}
#endregion ��������toString()
#region Double
/// <summary>
/// �ж��Ƿ��� ʵ��
/// </summary>
/// <param name="strnum"></param>
/// <returns></returns>
public static bool IsReal(string strnum)
{
bool result = false;
try
{
double curin = Convert.ToDouble(strnum);
result = true;
}
catch
{
result = false;
}
return result;
}
/// <summary>
/// string ת��Ϊ ˫����ʵ����ʾ
/// </summary>
/// <param name="inStr"></param>
/// <returns></returns>
public static double ToDouble(string str)
{
if (str == null || str == "")
return 0;
else
{
double db = 0;
if (Double.TryParse(str, out db) == false)
return 0;
else
return db;
}
}
public static double ToDouble(object obj)
{
if (obj == null || obj.ToString() == "")
return 0;
else
{
double db = 0;
if (Double.TryParse(obj.ToString(), out db) == false)
return 0;
else
return db;
}
}
#endregion Double
#region �ַ�����ʽ��
/// <summary>
/// ���ַ���
/// </summary>
/// <param name="str"></param>
/// <param name="spaceNum"></param>
/// <returns></returns>
[Obsolete("�������� ����ʹ�� LeftSpace")]
public static string LeftNull(int num)
{
string curStr = "";
for (int i = 0; i < num; i++)
{
curStr += @" ";
}
return curStr;
}
/// <summary>
///���ַ���
/// </summary>
/// <param name="spaceNum">���ַ��� ����</param>
/// <returns></returns>
public static string LeftSpace(int spaceNum)
{
string curStr = "";
for (int i = 0; i < spaceNum; i++)
{
curStr += @" ";
}
return curStr;
}
/// <summary>
/// ���߼ӿո�
/// </summary>
/// <param name="str"></param>
/// <param name="num"></param>
/// <returns></returns>
[Obsolete("�������� ����ʹ�� LeftSpaceStr")]
public static string LeftNullStr(string str, int num)
{
string curStr = "";
int Len = str.Length;
if (Len > num)
{
return str.Substring(Len - num, num);
}
else
{
curStr = LeftSpace(num - Len) + str;
return curStr;
}
}
/// <summary>
/// ���߿��ַ���
/// </summary>
/// <param name="str"></param>
/// <param name="spaceNum"></param>
/// <returns></returns>
public static string LeftSpaceStr(string str, int spaceNum)
{
int Len = str.Length;
if (Len > spaceNum)
{
return str.Substring(Len - spaceNum, spaceNum);
}
else
{
return LeftSpace(spaceNum - Len) + str;
}
}
/// <summary>
/// ���߿��ַ���
/// </summary>
/// <param name="inNumber"></param>
/// <param name="spaceNum"></param>
/// <returns></returns>
public static string LeftSpaceStr(int inNumber, int spaceNum)
{
string str = inNumber.ToString();
return LeftSpaceStr(str, spaceNum);
}
/// <summary>
/// ��������0
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static string Leftzero(int num)
{
string curStr = "";
for (int i = 0; i < num; i++)
{
curStr += "0";
}
return curStr;
}
/// <summary>
/// ��������0
/// </summary>
/// <param name="str"></param>
/// <param name="formatLen"></param>
/// <returns></returns>
public static string LeftzeroStr(string str, int formatLen)
{
int len = str.Length;
if (len >= formatLen)
{
return str.Substring(len - formatLen, formatLen);
}
else
{
return Leftzero(formatLen - len) + str;
}
}
/// <summary>
/// ��ʽ�� �̶�λ���ַ� ������0����
/// </summary>
/// <param name="inNum"></param>
/// <param name="formatLen"></param>
/// <returns></returns>
public static string LeftzeroStr(int inNum, int formatLen)
{
string str = inNum.ToString();
return LeftzeroStr(str, formatLen);
}
#endregion �ַ�����ʽ��
#region ��ʽ����׼����
private const int NumNumber = 0;
private const int priceNumber = 2;
private const int MoneyNumber = 2;
#region Number
public static string Number(object numStr)
{
return KeepDigit(FormatCom.ToDecimal(numStr), NumNumber);
}
/// <summary>
/// ˫�����ַ�ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ����������</param>
/// <returns></returns>
public static string Number(double numStr)
{
return KeepDigit((decimal)numStr, NumNumber);
}
/// <summary>
/// ˫�����ַ�ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ����������</param>
/// <returns></returns>
public static string Number(System.Nullable<decimal> numStr)
{
decimal curNum = 0;
if (numStr != null)
{
curNum = (decimal)numStr;
}
return KeepDigit(curNum, NumNumber);
}
/// <summary>
/// ʮ�����ַ�ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ����������</param>
/// <returns></returns>
public static string Number(decimal numStr)
{
return KeepDigit(numStr, NumNumber);
}
/// <summary>
/// �����ַ���ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ����������</param>
/// <returns></returns>
public static string Number(string numStr)
{
return KeepDigit(numStr, NumNumber);
}
#endregion Number
#region price �۸���ʽ��
public static string Price(object numStr)
{
return KeepDigit(FormatCom.ToDecimal(numStr), priceNumber);
}
/// <summary>
/// ˫�����ַ�ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ���и�ʽ��������</param>
/// <returns></returns>
public static string Price(double numStr)
{
return KeepDigit((decimal)numStr, priceNumber);
}
/// <summary>
/// ˫�����ַ�ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ���и�ʽ��������</param>
/// <returns></returns>
public static string Price(System.Nullable<decimal> numStr)
{
decimal curNum = 0;
if (numStr != null)
{
curNum = (decimal)numStr;
}
return KeepDigit(curNum, priceNumber);
}
/// <summary>
/// ʮ�����ַ�ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ���и�ʽ��������</param>
/// <returns></returns>
public static string Price(decimal numStr)
{
return KeepDigit(numStr, priceNumber);
}
/// <summary>
/// �����ַ���ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ���и�ʽ���������ַ���</param>
/// <returns></returns>
public static string Price(string numStr)
{
return KeepDigit(numStr, priceNumber);
}
#endregion price �۸���ʽ��
#region money ����
public static string Money(object numStr)
{
return KeepDigit(FormatCom.ToDecimal(numStr), MoneyNumber);
}
/// <summary>
/// ˫�����ַ�ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ���и�ʽ��������</param>
/// <returns></returns>
public static string Money(double numStr)
{
return KeepDigit((decimal)numStr, MoneyNumber);
}
/// <summary>
/// ʮ�����ַ�ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ���и�ʽ��������</param>
/// <returns></returns>
public static string Money(System.Nullable<decimal> numStr)
{
return KeepDigit((decimal)numStr, MoneyNumber);
}
/// <summary>
/// ʮ�����ַ�ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ���и�ʽ��������</param>
/// <returns></returns>
public static string Money(decimal numStr)
{
return KeepDigit(numStr, MoneyNumber);
}
/// <summary>
/// �����ַ�ת��Ϊ������ʽ�ַ�����ʾ
/// </summary>
/// <param name="numStr">��Ҫ���и�ʽ���������ַ���</param>
/// <returns></returns>
public static string Money(string numStr)
{
return KeepDigit(numStr, MoneyNumber);
}
#endregion money ����
#region ��ʽ������
/// <summary>
/// ����С��λ��������String��
/// </summary>
/// <param name="numStr">�ַ���</param>
/// <param name="count">�����</param>
public static string KeepDigit(string numStr, int count)
{
string s = numStr;
string Formatstr = "0.";
decimal d = 0;
decimal.TryParse(s.Trim(), out d);
for (int i = 0; i < count; i++)
{
Formatstr += "0";
}
return d.ToString(Formatstr);
}
/// <summary>
/// ����С��λ��������decimal��
/// </summary>
/// <param name="inDecdecimal">ʮ��������</param>
/// <param name="count">�����</param>
public static string KeepDigit(decimal inDecdecimal, int count)
{
string Formatstr = "0.";
for (int i = 0; i < count; i++)
{
Formatstr += "0";
}
return inDecdecimal.ToString(Formatstr);
}
/// <summary>
///
/// </summary>
/// <param name="indouble"></param>
/// <param name="count"></param>
/// <returns></returns>
public static string KeepDigit(double indouble, int count)
{
string Formatstr = "0.";
for (int i = 0; i < count; i++)
{
Formatstr += "0";
}
return indouble.ToString(Formatstr);
}
#endregion ��ʽ������
#endregion ��ʽ����׼����
}
}