自己的小代码整理库

来源:互联网 发布:java 简易画板 编辑:程序博客网 时间:2024/05/01 14:10
  1. 1. /// 将字母,数字由全角转化为半角 
  2.          /// </summary> 
  3.          /// <returns></returns> 
  4.          public string NarrowToSmall(string inputString) 
  5.          { 
  6.               char[] c = inputString.ToCharArray(); 
  7.               for (int i = 0; i < c.Length; i++) 
  8.               { 
  9.                    byte[] b = System.Text.Encoding.Unicode.GetBytes(c,i,1); 
  10.                    if (b.Length == 2) 
  11.                    { 
  12.                        if (b[1] == 255) 
  13.                        { 
  14.                             b[0] = (byte)(b[0] + 32); 
  15.                             b[1] = 0; 
  16.                             c[i] = System.Text.Encoding.Unicode.GetChars(b)[0]; 
  17.                        } 
  18.                    } 
  19.               } 
  20.               string returnString = new string(c); 
  21.               return returnString;   // 返回半角字符 
  22.          } 
  23.          /// <summary> 
  24.          /// 将字母,数字由半角转化为全角 
  25.          /// </summary> 
  26.          /// <param name="inputString"></param> 
  27.          /// <returns></returns> 
  28.          public string  NarrowToBig(string inputString) 
  29.          { 
  30.               char[] c = inputString.ToCharArray(); 
  31.               for (int i = 0; i < c.Length; i++) 
  32.               { 
  33.                    byte[] b=System.Text.Encoding.Unicode.GetBytes(c, i, 1); 
  34.                    if (b.Length == 2) 
  35.                    { 
  36.                        if (b[1] == 0) 
  37.                        { 
  38.                             b[0] = (byte)(b[0] - 32); 
  39.                             b[1] = 255; 
  40.                             c[i] = System.Text.Encoding.Unicode.GetChars(b)[0]; 
  41.                        } 
  42.                    } 
  43.               } 
  44.               string returnString = new string(c); 
  45.               return returnString;   // 返回全角字符 
  46.          } 
  47.      } 
  48. 2.   数字
  49.      public bool dayIsNumeric(string strCode)
  50.         {
  51.             if (strCode == null || strCode.Length != 2)
  52.             {
  53.                 return false;
  54.             }
  55.             ASCIIEncoding ascii = new ASCIIEncoding();
  56.             byte[] byteStr = ascii.GetBytes(strCode);
  57.             foreach (byte code in byteStr)
  58.             {
  59.                 if (code < 48 || code > 57)
  60.                     return false;
  61.             }
  62.             
  63.             return true;
  64.         }
  65.         
  66.         
  67. 3 http://www.96yx.com/tool/ASC2.htm 码值、
  68. 4.递归最大最小值
  69. int Max(int[] a, int b)
  70.         {
  71.             if (b < a.Length-1)
  72.             {
  73.                 return a[b] < Max(a, b + 1) ? Max(a, b + 1) : a[b];
  74.             }
  75.             return a[b];
  76.         }
  77.  int Min(int[] a, int b)
  78.         {
  79.             if (b < a.Length-1)
  80.             {
  81.                 return a[b] > Min(a, b + 1) ? Min(a, b + 1) : a[b];
  82.             }
  83.             return a[b];
  84.         }

四舍五入

   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);

原创粉丝点击