C#值数值类型转换

1.十进制转16进制

1 int number=10
2 string result=number.ToString("X2");
3 >>0A
4 //X2表示大写2位

 

2.字符串转数值类型

1 int data = int.Parse("99");

 

3.16进制的字符串转数值类型

注:16进制字符串不能直接转换,因为无法识别当中的字符a~f

1 int data = int.Parse("a9", System.Globalization.NumberStyles.AllowHexSpecifier);//指示数值字符串标识16进制
2 3 int data = Convert.ToInt16("a9", 16);

 

 

3.科学计数转换为数值类型

将 "9.820E+05" 与 "1.009E-05"转换为数值类型,直接 使用 Convert.ToDouble 的方法

1  string strNumber1 = "9.820E+05";
2  string strNumber2 = "1.009E-05";
3  
4  double data1 = Convert.ToDouble(strNumber1);            
5  double data2 = Convert.ToDouble(strNumber2)

 

4.16进制字符串转换为数值并按位比较

1 temp = strTemp.Substring(20, 2);
2 int number = int.Parse(strTemp, System.Globalization.NumberStyles.AllowHexSpecifier);
3 bool status=(number & 0x8000) == 0 ? false : true;

 或者

1     char temp = Convert.ToChar(Data.Substring(4, 1));  
2     if ((temp1 & 2) != 0)
3         result += "第3个字节的高四位为0010";

 

5.List数组转换

List<decimal> prices = t.Split(',').Select(n => decimal.Parse(n, style)).ToList();

或者

1  string[] arr =["1","2"];
2  List<int> intList = Array.ConvertAll<string,int>(arr,s=>int.Parse(s)).ToList();

 

6.保留两位小数

 1     double a = 123456, b = 123456.1, c = 123456.12, d = 123456.123, e = 123456.126;
 2     Console.WriteLine(a.ToString("N"));  //123,456.00
 3     Console.WriteLine(b.ToString("N"));  //123,456.10
 4     Console.WriteLine(c.ToString("N"));  //123,456.12
 5     Console.WriteLine(d.ToString("N"));  //123,456.12
 6     Console.WriteLine(e.ToString("N"));  //123,456.13
 7     Console.WriteLine();
 8 
 9     Console.WriteLine(a.ToString("N2"));  //123,456.00
10     Console.WriteLine(b.ToString("N2"));  //123,456.10
11     Console.WriteLine(c.ToString("N2"));  //123,456.12
12     Console.WriteLine(d.ToString("N2"));  //123,456.12
13     Console.WriteLine(e.ToString("N2"));  //123,456.13
14     Console.WriteLine();
15 
16     Console.WriteLine(a.ToString("F2"));  //123456.00
17     Console.WriteLine(b.ToString("F2"));  //123456.10
18     Console.WriteLine(c.ToString("F2"));  //123456.12
19     Console.WriteLine(d.ToString("F2"));  //123456.12
20     Console.WriteLine(e.ToString("F2"));  //123456.13
21     Console.WriteLine();
22 
23     Console.WriteLine(a.ToString("0.00"));  //123456.00
24     Console.WriteLine(b.ToString("0.00"));  //123456.10
25     Console.WriteLine(c.ToString("0.00"));  //123456.12
26     Console.WriteLine(d.ToString("0.00"));  //123456.12
27     Console.WriteLine(e.ToString("0.00"));  //123456.13
28     Console.WriteLine();
29 
30     Console.WriteLine(a.ToString("G"));  //123456
31     Console.WriteLine(b.ToString("G"));  //123456.1
32     Console.WriteLine(c.ToString("G"));  //123456.12
33     Console.WriteLine(d.ToString("G"));  //123456.123
34     Console.WriteLine(e.ToString("G"));  //123456.126
35     Console.WriteLine();
36 
37     //本地货币
38     Console.WriteLine(a.ToString("C"));  //NT$123,456
39     Console.WriteLine(b.ToString("C"));  //NT$123,456.1
40     Console.WriteLine(c.ToString("C"));  //NT$123,456.12
41     Console.WriteLine(d.ToString("C"));  //NT$123,456.123
42     Console.WriteLine(e.ToString("C"));  //NT$123,456.126
43     Console.WriteLine();
44     Console.Read();

 

posted @ 2016-06-28 11:20  imstrive  阅读(2507)  评论(0编辑  收藏  举报