自己的小代码整理库
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);
/// </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);
相关文章推荐
- JS代码大全(都是网上看到自己整理的)
- [置顶] 整理一些代码__xu_init.js 作为自己项目开发时使用
- 自己整理的iOS代码编写规范
- 自己收集整理的 DELPHI 中控制与使用HMTL帮助文件的单元代码。
- 在windows和Linux上安装ImageMagick与jmagick,Maven配置、Java图片压缩代码(整理网上、结合自己情况、编写出来的新安装方式)
- 标准和 JM 代码--根据firsttime的材料整理出自己需要的加了红色标记
- 自己的Matlab代码整理
- 在windows和Linux上安装ImageMagick与jmagick,Maven配置、Java图片压缩代码(整理网上、结合自己情况、编写出来的新安装方式)
- 自己的技术经验和心得,及常用的资料和代码如何整理、分类和保存才好呢?
- 自己整理的一些.net实用的代码!
- Android代码中自己写的一些工具类(整理)
- JAVA学习(十二)__关于JAVA代码里黄色警告线的注意方法(自己整理)
- TCP--UDP常用代码(socket编程--网上搜索自己整理的)
- 自己整理的IOS网络检测和版本检测代码
- JS代码大全 (都是网上看到 自己整理的)
- 将自己代码整理成cocoapods管理
- 自己收集整理的微软错误代码大全(中文和英文)
- 一些自己整理实用代码
- 自己整理的IOS网络检测和版本检测代码
- 自己收集整理的微软错误代码大全(中文和英文)