您的位置:首页 > 编程语言

自己的小代码整理库

2008-11-14 10:31 148 查看
1. /// 将字母,数字由全角转化为半角

/// </summary>

/// <returns></returns>

public string NarrowToSmall(string inputString)

{

char[] c = inputString.ToCharArray();

for (int i = 0; i < c.Length; i++)

{

byte[] b = System.Text.Encoding.Unicode.GetBytes(c,i,1);

if (b.Length == 2)

{

if (b[1] == 255)

{

b[0] = (byte)(b[0] + 32);

b[1] = 0;

c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];

}

}

}

string returnString = new string(c);

return returnString; // 返回半角字符

}

/// <summary>

/// 将字母,数字由半角转化为全角

/// </summary>

/// <param name="inputString"></param>

/// <returns></returns>

public string NarrowToBig(string inputString)

{

char[] c = inputString.ToCharArray();

for (int i = 0; i < c.Length; i++)

{

byte[] b=System.Text.Encoding.Unicode.GetBytes(c, i, 1);

if (b.Length == 2)

{

if (b[1] == 0)

{

b[0] = (byte)(b[0] - 32);

b[1] = 255;

c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];

}

}

}

string returnString = new string(c);

return returnString; // 返回全角字符

}

}

2. 数字

public bool dayIsNumeric(string strCode)

{

if (strCode == null || strCode.Length != 2)

{

return false;

}

ASCIIEncoding ascii = new ASCIIEncoding();

byte[] byteStr = ascii.GetBytes(strCode);

foreach (byte code in byteStr)

{

if (code < 48 || code > 57)

return false;

}

return true;

}

3 http://www.96yx.com/tool/ASC2.htm 码值、

4.递归最大最小值

int Max(int[] a, int b)

{

if (b < a.Length-1)

{

return a[b] < Max(a, b + 1) ? Max(a, b + 1) : a[b];

}

return a[b];

}

int Min(int[] a, int b)

{

if (b < a.Length-1)

{

return a[b] > Min(a, b + 1) ? Min(a, b + 1) : a[b];

}

return a[b];

}

四舍五入
private double Round(double v, int x)


{


bool isNegative = false;


//如果是负数


if (v < 0)


{


isNegative = true;


v = -v;


}


int IValue = 1;


for (int i = 1; i <= x; i++)


{


IValue = IValue * 10;


}


double Int = Math.Round(v * IValue + 0.5,0);


v = Int / IValue;




if (isNegative)


{


v = -v;


}


return v;


}

取负数
decimal a=5.0m;
a = decimal.Negate(a);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: